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.

190 questions
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,757
  • 2
  • 24
  • 30
6
votes
1 answer

scala/akka/stm design for large shared state?

I am new to Scala and Akka and am considering using it to solve a problem. Suppose I have a calculation engine (that searches for a solution). I'd like to parallelize that search both across cpus and across nodes by giving each cpu on each node its…
AllanC
  • 61
  • 1
6
votes
1 answer

Safe to use unsafeIOToSTM to read from database?

In this pseudocode block: atomically $ do if valueInLocalStorage key then readValueFromLocalStorage key else do value <- unsafeIOToSTM $ fetchValueFromDatabase key writeValueToLocalStorage key value Is it safe to use…
Philip Kamenarsky
  • 2,757
  • 2
  • 24
  • 30
6
votes
1 answer

Haskell, Channels, STM, -threaded, Message Passing

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…
Jonathan Gallagher
  • 2,115
  • 2
  • 17
  • 31
6
votes
1 answer

Validation in STM transactions nested with orElse

This commentary page describes a lot of the fine details of STM in GHC, but I'd like clarity on a couple points. First, is a nested transaction invalidated when variables accessed in the parent change? For instance we have in thread A: takeTMVar a…
jberryman
  • 16,334
  • 5
  • 42
  • 83
6
votes
2 answers

STM.NET versus Clojure STM

I'm wondering how is that possible that Clojure has implemented Software Transactional Memory and doesn't see any problem with that while Microsoft didn't finish its work for C# and noticed some problems which make it unpractical to implement STM…
user1121956
  • 1,843
  • 2
  • 20
  • 34
6
votes
3 answers

STM hash library for C (glib?)

I'm looking for some C library that includes STM-style (Software Transactional Memory) hash maps, but I had no luck so far. It would be great if it was based on glib / gobject, but it's not that crucial. It also doesn't need proper transactions over…
viraptor
  • 33,322
  • 10
  • 107
  • 191
6
votes
1 answer

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

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…
Thomas Heywood
  • 903
  • 2
  • 9
  • 18
5
votes
1 answer

TransactionalMap vs SynchronizedMap

When should one prefer Akka TransactionalMap over regular immutable map mixed in with SynchronizedMap and vice versa? (Or am I comparing apples to oranges here?)
missingfaktor
  • 90,905
  • 62
  • 285
  • 365
5
votes
1 answer

Testing STM functions purely

I've recently started using STM for some bits in a project of mine, but I'm having trouble figuring out how to test it. I have no IO in these functions and was hoping I could write QuickCheck properties to test things, but "atomically" (STM a -> IO…
Adam Wagner
  • 15,469
  • 7
  • 52
  • 66
5
votes
1 answer

STM and outgoing IO

If I am inside a STM whose transaction fail, and I retry as part of the normal control flow (no STM collision etc..), I might want to indicate to someone outside a way to take corrective action. If it's purely 'outgoing', then my STM can still be…
nicolas
  • 9,549
  • 3
  • 39
  • 83
5
votes
1 answer

Does STM provide fine-grained locking for existing data structures?

Reading Bartosz Milewski's fantastic blog post on STM, I was excited to read the following: But take into account an important fact: STM is very fine grained. For instance, when you’re inserting an item into a tree, the STM transaction will…
Daniel Buckmaster
  • 7,108
  • 6
  • 39
  • 57
5
votes
1 answer

Possible improvements in clojure "official" concurrency example (using locks,atoms,stm)

i am trying to get the "official" example of clojure concurrency closer to a java version using manual locking. In this gist i put the java and clojure code and the thread dump of a VisualVm profile of all versions. Here it is the clojure code and…
jneira
  • 944
  • 10
  • 18
5
votes
1 answer

What is wrong with the following solution to the "Dining Philosophers"?

In order to get familiar with STM in Haskell, I wrote the following solution to the Dining Philosophers problem: import Control.Concurrent import Control.Concurrent.STM import Control.Monad import System.Random type Fork = TVar Bool type…
Alexandros
  • 3,044
  • 1
  • 23
  • 37
5
votes
2 answers

Haskell: Updating two or more TVars atomically. Possible?

Can one transaction update two different TVars in an atomic way? i.e. can I compose data structures out of lots of TVars to reduce contention? If so, could you provide an example?
Clinton
  • 22,361
  • 15
  • 67
  • 163