stm

Using TChan with Timeout

被刻印的时光 ゝ 提交于 2019-12-21 10:49:58
问题 I have a TChan as input for a thread which should behave like this: If sombody writes to the TChan within a specific time, the content should be retrieved. If there is nothing written within the specified time, it should unblock and continue with Nothing . My attempt on this was to use the timeout function from System.Timeout like this: timeout 1000000 $ atomically $ readTChan pktChannel This seemed to work but now I discovered, that I am sometimes loosing packets (they are written to the

Haskell fast concurrent queue

隐身守侯 提交于 2019-12-20 08:58:24
问题 The Problem Hello! I'm writing a logging library and I would love to create a logger, that would run in separate thread, while all applications threads would just send messages to it. I want to find the most performant solution for this problem. I need simple unboud queue here. Approaches I've created some tests to see how available solutions perform and I get very strange results here. I tested 4 implementations (source code provided below) based on: pipes-concurrency Control.Concurrent.Chan

How can I see the number of rollbacks in my STM in Clojure?

夙愿已清 提交于 2019-12-19 05:15:10
问题 How can I see the number of rollbacks in my STM in Clojure? 回答1: You can't... unless you are willing to cheat: (defmacro spy-dosync [& body] `(let [retries# (atom -1) result# (dosync (swap! retries# inc) ~@body)] (println "retries count:" @retries#) result#)) and then replace your dosync by a spy-dosync. 回答2: If you're feeling frisky, you could hack the Clojure source and rebuild (it's easy to rebuild the Clojure source). Transaction retries happen in src/jvm/clojure/lang/LockingTransaction

JSP、servlet、Tomcat

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 20:26:24
jsp动作:<jsp:include page="" flush=“true”> jsp指令:<%@include file=“a.jsp”%> 动作是在页面被访问时导入的,指令是在编译时导入的; 在指令中会编译为一个servlet类文件,在动作中会分为两个所以效率稍慢; <jsp:forward page="" > 跳转后,地址栏不变; page只能是web应用中的文件; jsp指令:page、include、taglib jsp动作:include、forward、param等等 JSP元素可以分为三大类:JSP指令元素、JSP动作元素、JSP脚本元素。 一、JSP指令元素仅仅是在“翻译”阶段使用的,即把JSP翻译成Servlet。 jsp内置对象:request、response、session、application、out、page、config; request: request.getRemoteAddress()获取用户IP request.getParameter(“name”)获取参数 request.setCharacterEncoding(“utf-8”) 请求头: accept:客户端能接收的MIME类型 accept-language:浏览器的首选语言 user-agent:客户端程序的相关信息;浏览器版本、系统版本等; host:服务器的主机号和端口号

How to handle or avoid BlockedIndefinitelyOnSTM exception?

◇◆丶佛笑我妖孽 提交于 2019-12-10 16:53:19
问题 I spent quite a lot of time troubleshooting an issue I had in the application I am working on. This application is a web app, exposing REST endpoints using scotty. It uses a TVar to hold its state which is updated through STM a actions triggered by the front-end layer. As this application is based on event sourcing principles, any event generated by business layer after STM transactions complete is stored into an EventStore (currently a simple flat file...). Here is the relevant code fragment

Are refs really consistent within a STM transaction?

二次信任 提交于 2019-12-10 16:20:37
问题 I read at clojure.org/refs that All reads of Refs will see a consistent snapshot of the 'Ref world' as of the starting point of the transaction (its 'read point'). The transaction will see any changes it has made. This is called the in-transaction-value. There's also a link to Snapshot Isolation on wikipedia that implies that reads of any number of refs will be consistent with each other once a transaction has started. I made a test case... (def r1 (ref 0)) (def r2 (ref 0)) (defn delay-then

Should multiple Clojure refs be read in a transaction for consistency?

感情迁移 提交于 2019-12-10 14:12:10
问题 This is a theoretical question motivated by my desire to understand Clojure's concurrency better. Let's say I'm writing boids. Assume each boid is a separate green thread mutating positions in a vector or refs representing a world grid. Think Hickey's ants colony. Now, documentation at Clojure.org states "All reads of Refs will see a consistent snapshot of the ‘Ref world’ as of the starting point of the transaction (its ‘read point’)." Does this mean I can only get a consistent snapshot of my

Can one monitor STM's contention level?

。_饼干妹妹 提交于 2019-12-09 18:01:11
问题 Is there any way to poll whether Clojure's STM transactions are being retried, and at what rate? 回答1: You can observe the history count of a ref which will indicate that there is contention on it: user=> (def my-ref (ref 0 :min-history 1)) #'user/my-ref user=> (ref-history-count my-ref) 0 user=> (dosync (alter my-ref inc)) 1 user=> (ref-history-count my-ref) 1 The history count does not directly represent contention. Instead it represents the number of past values that have been maintained in

Haskell: TVar: orElse

会有一股神秘感。 提交于 2019-12-09 03:29:15
问题 Is the "else" part of orElse called when a transaction is retried due to another transaction writing to a TVar it had read, or only when retry is explicitly called? 回答1: If you have orElse a b then b is only run if retry is called explicitly in a . Otherwise orElse would essentially become nondeterministic. (The rerunning of transactions that is done by the STM runtime is transparent and should not effect the outcome of any computation.) 来源: https://stackoverflow.com/questions/10101044

Haskell, Channels, STM, -threaded, Message Passing

岁酱吖の 提交于 2019-12-07 05:47:45
问题 I am trying to use channels/STM to implement message passing in Haskell. Maybe this is a terrible idea, and there is a better way to implement/use message passing in Haskell. If this is the case, do let me know; however, my quest has opened some basic questions on concurrent Haskell. I have heard great things about STM, and in particular the implementation in Haskell. Since it supports reading to and writing from, and has some safety benefits, I figured one would start there. This brings up