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
4
votes
2 answers

Large Data Structure in Clojure STM for Banking Transactions LOOP

I am new to Functional Programming and Clojure, so I am not really sure about what to do for a project at University. The project should show the advantage of Clojure STM for banking transactions (Transfer of money from account A to account B). So I…
nuvio
  • 2,555
  • 4
  • 32
  • 58
4
votes
3 answers

Readers-writers using STM in Clojure

There is the following version of readers-writers problem: multiple readers and writers, 2 or more readers can read simultaneously, if a writer is writing no one can read or write, it is preferred if all writers get an equal chance to write (for…
bvk256
  • 1,837
  • 3
  • 20
  • 38
4
votes
1 answer

Why does this TCP server immediately exit?

This is an extraction I did from a larger project, which didn't seem to have the issue of the server immediately returning (I'll admit the reason I did the extraction in the first place was in the hope of asking a different question regarding…
bbarker
  • 11,636
  • 9
  • 38
  • 62
4
votes
3 answers

STM and alter in clojure

I am working through the Programming Clojure book. While explaining alter and the STM, they say that if, during an alter, Clojure detects a change to the ref from outside the transaction, it will re-run the transaction with the new value. If that is…
Matt Briggs
  • 41,224
  • 16
  • 95
  • 126
4
votes
1 answer

Why is an atomic block in Scala run twice when exception gets thrown?

Code: object Blub { import scala.concurrent.stm._ val last = Ref("none") def bla() = atomic { implicit txn => last() = "outer" try { atomic { implicit txn => last() = "inner" println(last()) throw new…
Lohmi
  • 73
  • 5
4
votes
1 answer

Memory leak when using Control.Concurrent.STM.TBQueue

I have two threads: producer and consumer. Producer produces some (key,value) pairs and consumer inserts them into Map, wrapped into Data.IORef. I tried to use Control.Concurrent.BoundedChan for communication between producer and consumer, and it…
user2155932
  • 760
  • 3
  • 9
4
votes
1 answer

Race condition in Clojure stm?

Hello I was reading the book joy of clojure and in the section about the STM they have an image of 2 transactions where A is initially retrieving the same value from a ref as B is and then both transaction A and B does their calculations but A…
Tobias
  • 236
  • 2
  • 6
4
votes
2 answers

Clojure commute and alter performance

There is a little bit modified example from clojure.org/refs (defn mod-nth [v i f] (assoc v i (f (v i)))) (defn run [oper nvecs nitems nthreads niters] (let [vec-refs (vec (map (comp ref vec) (partition nitems (repeat (*…
Odomontois
  • 15,918
  • 2
  • 36
  • 71
4
votes
1 answer

How to handle or avoid BlockedIndefinitelyOnSTM exception?

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…
insitu
  • 4,488
  • 3
  • 25
  • 42
4
votes
1 answer

Haskell one way `dupTChan`

Is there any function like dupTChan except that when you do: newChan = dupTChanOneWay oldChan Anything written to oldChan is written to newChan, but not the other way around?
PyRulez
  • 10,513
  • 10
  • 42
  • 87
4
votes
2 answers

Agent not getting executed

I have a series of functions (like some-operation in the example), which I send or send-off to agents: (defn some-operation [agent-state] (dosync (let [updated (foo agent-state)] ;; derive new state from old one (alter bar whatev updated)…
deprecated
  • 5,142
  • 3
  • 41
  • 62
4
votes
2 answers

Clojure STM ambiguity factor

In Clojure we use STM for Concurrency. My question is STM uses point in time value of data , isn't this introduce ambiguity ? How could we know what value is accessed ?
nish1013
  • 3,658
  • 8
  • 33
  • 46
4
votes
1 answer

Haskell: TChan wrapping duplicated while using lens to modify state

Consider the following code (and error): import Control.Lens import Control.Monad.STM import Control.Monad.IO.Class import Control.Monad.State.Lazy import Control.Concurrent.STM.TChan data Broadcast = Broadcast Int data ImmutableState =…
kvanbere
  • 3,289
  • 3
  • 27
  • 52
4
votes
1 answer

Is there a bug in this clojure solution to sleeping barber?

This is presented as a solution to the sleeping barber problem. (Attributed to CGrand, but I found the reference here) I'm curious about the dosync block in enter-the-shop. My understanding is that this is a transaction, and so empty-seats will…
Dax Fohl
  • 10,654
  • 6
  • 46
  • 90
4
votes
1 answer

Are refs really consistent within a STM transaction?

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…
sw1nn
  • 7,278
  • 1
  • 26
  • 36