stm

Canonical Way to Ensure Only One Instance of a Service Is Running / Starting / Stopping in Clojure?

ぐ巨炮叔叔 提交于 2019-12-05 18:27:36
I'm writing a stateful server in Clojure backed by Neo4j that can serve socket requests, like HTTP. Which means, of course, that I need to be able to start and stop socket servers from within this server. Design-wise, I would want to be able to declare a "service" within this server and start and stop it. What I'm trying to wrap my mind around in Clojure is how to ensure that starting and stopping these services is thread-safe. This server I'm writing will have NREPL embedded inside it and process incoming requests in a parallel way. Some of these requests will be administrative: start service

Haskell: thread blocked indefinitely in an STM transaction

折月煮酒 提交于 2019-12-05 18:14:56
问题 Is there any way to increase a time interval, on the basis of which the RTS decides that thread has blocked indefinitely in an STM transaction? Here is my code: import Control.Concurrent (ThreadId) import Control.Concurrent.MVar (MVar,newMVar,withMVar) import Control.Concurrent.STM import qualified Control.Concurrent.ThreadManager as TM data ThreadManager = ThreadManager { tmCounter::TVar Int, tmTM::MVar TM.ThreadManager } data Settings = Settings { maxThreadsCount::Int } createThreadManager

Clojure STM ( dosync ) x Java synchronize block

会有一股神秘感。 提交于 2019-12-04 18:06:16
问题 What is the difference between Clojure STM (dosync) approach and Java synchronize Block? I'm reading the code below from "The sleeping barber" problem. (http://www.bestinclass.dk/index.clj/2009/09/scala-vs-clojure-round-2-concurrency.html) (defn the-shop [a] (print "[k] entering shop" a) (dosync (if (< (count @queue) seats) (alter queue conj a) (print "[s] turning away customer" a)))) To avoid race conditions, dosync is used, so i ask myself "What is the difference (STM) from Java synchronize

java Spring定时器 每个季度执行一次

纵饮孤独 提交于 2019-12-04 10:46:51
@Scheduled(cron = " 0 00 00 1 4,7,10,1 ?")//每个季度的第一天零点进行统计此注解是每个季度结束后的下一天执行(因为Spring不识别字母(L---每月最后一天))所以只能采用下个月的第一天以下代码针对时间进行处理 Calendar cal = Calendar.getInstance();//当前时间int month = cal.get(Calendar.MONTH) + 1;//当前月份int year = cal.get(Calendar.YEAR);//当前年int quarter=0;//当前季度int stm = 1;//季度的起始月份int etm = 3;//季度的结束月份switch (month){ case 1: stm = 10; etm=12; quarter=3; year=year-1; break;//因为一月份进入方法是统计上一年第四季度的信息,所以处理年份信息 case 10: stm = 7; etm=9; quarter=2; break;//本年 case 7: stm = 4; etm=6; quarter=1; break;//本年 case 4: stm = 1; etm=3; quarter=0; break;//本年}最终得到 year---年份     quarter--季度    

Using TChan with Timeout

半腔热情 提交于 2019-12-04 04:53:39
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 channel, but not read on the other side. In the log I get this: 2014.063.11.53.43.588365 Pushing Recorded

Can one monitor STM's contention level?

末鹿安然 提交于 2019-12-04 04:04:39
Is there any way to poll whether Clojure's STM transactions are being retried, and at what rate? 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 order to service concurrent reads. The size of the history is limited by min and max values. By default

Clojure STM ( dosync ) x Java synchronize block

人盡茶涼 提交于 2019-12-03 11:43:34
What is the difference between Clojure STM (dosync) approach and Java synchronize Block? I'm reading the code below from "The sleeping barber" problem. ( http://www.bestinclass.dk/index.clj/2009/09/scala-vs-clojure-round-2-concurrency.html ) (defn the-shop [a] (print "[k] entering shop" a) (dosync (if (< (count @queue) seats) (alter queue conj a) (print "[s] turning away customer" a)))) To avoid race conditions, dosync is used, so i ask myself "What is the difference (STM) from Java synchronize block" ? Will it block this critical code ? Thanks in advance ! Dantas dosync and synchronized give

How should I make a clojure STM program persistent?

这一生的挚爱 提交于 2019-12-03 09:40:50
问题 I am writing a clojure program which uses the STM. At the moment I am populating the STM (using refs) at startup from a database, and then asynchronously updating the database whenever a dosync transaction succeeds. I have no idea if I am doing this the right way though, or if there is a better standard technique for doing this. Could anyone explain to me how they make the ACI properties of STM into ACID in their Clojure programs? 回答1: In general, adding the 'D' in ACID to any program is not

Poor performance / lockup with STM

送分小仙女□ 提交于 2019-12-03 06:53:34
问题 I'm writing a program where a large number of agents listen for events and react on them. Since Control.Concurrent.Chan.dupChan is deprecated I decided to use TChan's as advertised. The performance of TChan is much worse than I expected. I have the following program that illustrates the issue: {-# LANGUAGE BangPatterns #-} module Main where import Control.Concurrent.STM import Control.Concurrent import System.Random(randomRIO) import Control.Monad(forever, when) allCoords :: [(Int,Int)]

How do you implement Software Transactional Memory?

筅森魡賤 提交于 2019-12-03 05:45:38
问题 In terms of actual low level atomic instructions and memory fences (I assume they're used), how do you implement STM? The part that's mysterious to me is that given some arbitrary chunk of code, you need a way to go back afterwards and determine if the values used in each step were valid. How do you do that, and how do you do it efficiently? This would also seem to suggest that just like any other 'locking' solution you want to keep your critical sections as small as possible (to decrease the