Questions tagged [stm]

Software transactional memory (STM) is a mechanism for synchronization in concurrent programming, which can perform groups of memory operations atomically. Using transactional memory (implemented by optimistic synchronization) instead of locks removes the risk of a deadlock.

182 questions
9
votes
1 answer

Haskell STM check function returning undefined

Is there a good reason why the check function in the Contol.Concurent.STM library has type Bool -> STM a and returns undefined on success rather then having the type Bool -> STM ()? The way it is implemented the type checker will polity compile a…
John F. Miller
  • 25,556
  • 8
  • 66
  • 120
9
votes
1 answer

deref inside a transaction may trigger a retry - what is the role of ref state history?

"Clojure programming" (Emerick, O'Reilly) states, that: (...) if a new value if commited by another transaction since the beginning of the current transaction, the new value of the ref as of the start of the transaction cannot be provided.…
kamituel
  • 30,600
  • 3
  • 71
  • 91
9
votes
5 answers

Stop threads from interleaving output

The following program creates two threads running concurrently, that each sleep for a random amount of time, before printing a line of text to stdout. import Control.Concurrent import Control.Monad import System.Random randomDelay t = randomRIO (0,…
Chris Taylor
  • 44,831
  • 12
  • 101
  • 146
8
votes
3 answers

Use of agents to complete side-effects in STM transactions

I'm aware that it is generally bad practice to put functions with side-effects within STM transactions, as they can potentially be retried and called multiple times. It occurs to me however that you could use agents to ensure that the side effects…
mikera
  • 101,777
  • 23
  • 241
  • 402
8
votes
2 answers

Experiences with Clojure STM for large datasets?

I need to make a decision about whether to use STM in a Clojure system I am involved with for a system which needs several GB to be stored in a single STM ref. I would like to hear from anyone who has any advice in using Clojure STM with large…
yazz.com
  • 52,748
  • 62
  • 227
  • 363
8
votes
1 answer

Is it safe to use trace inside a STM stransaction?

I have a transaction failing indefinitely for some reason, and I would like to use trace instructions inside. For example, to print the state of the MVar's before executing the transaction in this fragment: data_out <- atomically $ do …
dsign
  • 11,152
  • 6
  • 51
  • 77
8
votes
2 answers

Why is the commute function called twice when changing a ref in Clojure?

I think I understand the basic difference between the ideas of commute and alter within a Clojure transaction. Alter essentially 'locks' the identity from the start to the end of the transaction so that multiple transactions must execute…
Sam
  • 954
  • 10
  • 18
8
votes
1 answer

Are TChan writes integrated into Haskell STM?

If an STM transaction fails and retries, does the call to writeTChan get re-executed so that you end up with two writes, or does the STM only actually perform the write if the transaction commits? i.e., is this solution to the sleeping barber…
Dax Fohl
  • 10,006
  • 5
  • 40
  • 82
7
votes
1 answer

Can Clojure's STM's history of values be accessed?

Given that the STM holds a history of say 10 values of refs, agents etc, can those values be read ? The reason is, I'm updating a load of agents and I need to keep a history of values. If the STM is keeping them already, I'd rather just use them. I…
Hendekagon
  • 4,405
  • 2
  • 25
  • 42
7
votes
3 answers

Strange behavior of clojure ref

I have 100 workers (agents) that share one ref that contains collection of tasks. While this collection have tasks, each worker get one task from this collection (in dosync block), print it and sometimes put it back in the collection (in dosync…
7
votes
2 answers

STM-friendly list as a change log

I need an advice on the data structure to use as an atomic change log. I'm trying to implement the following algorithm. There is a flow of incoming changes updating an in-memory map. In Haskell-like pseudocode it is update :: DataSet ->…
Mikhail Puzanov
  • 219
  • 1
  • 6
7
votes
3 answers

Can one monitor STM's contention level?

Is there any way to poll whether Clojure's STM transactions are being retried, and at what rate?
deprecated
  • 4,948
  • 3
  • 38
  • 58
7
votes
1 answer

STM with partial atomicity for certain TVars

I am doing things with STM and have among other things used the TBQueue data structure with great success. An useful feature I've been using it for involves reading from it based on a precondition in a TVar, basically like so: shouldRead <- readTVar…
dflemstr
  • 25,186
  • 5
  • 66
  • 102
7
votes
3 answers

Haskell: TVar: Preventing starvation

I'm considering using a TVar to store some state in a web application (that can be recreated on restart). However, the contention aspects of TVar concern me. It seems that a frequent short running transaction can starve out longer transactions by…
Clinton
  • 20,364
  • 13
  • 59
  • 142
6
votes
3 answers

Way to synchronize reads and writes in Clojure?

In a web app I'm trying to generate a unique thread safe id from a limited id pool. The problem I'm facing is that between reading and writing another thread may already have changed the data structure; this is why I have to resort to…
Philip Kamenarsky
  • 2,545
  • 2
  • 19
  • 29
1 2
3
12 13