5 ms·
Haskell noob here. How is STM used in Haskell? What is type-driven STM? In Clojure, I don't recommend it because it's Just Another Place To Stick State. It's c
by arohner 10y ago
Haskell noob here. How is STM used in Haskell? What is type-driven STM?
In Clojure, I don't recommend it because it's Just Another Place To Stick State. It's cool, but provides little semantic value over compare-and-set! on an atom.
- DigitalJack 10y agoAren't atoms a part of STM in clojure?
- hawkice 10y agoNah, you can't update two atoms and make sure they both change at the same time. That being said, 99% of the time you can have one atom, a map, with two keys, and that'll do it.
- eveningcoffee 10y agoNope, atoms are based on simple CAS (Compare And Swap) operation that replaces the reference value. They are pretty much a sugar around AtomicReference in Java (in case of JVM implementation of course). It is pretty much as like this 1. Dereference a variable and keep the old reference. 2. Create a new modified variable. 3. Use CAS on old reference and new variable to update the reference to point to the new vairable. In case of failure (some other thread made the update before) go to 1. But the version of SMT implemented in Closure is very similar to the CAS, just now you can apply it to the multiple variables i.e. it is multi variable CAS. It has the same drawbacks and advantages as atomic references: your code does not depend on lock taking order (as they are managed by the transaction manager) and you avoid dead locks as the locks are taken in the same order before applying the update, but now two different threads can start the transaction to update the same variable, but only one of them can finish as the first one and the other one should be cancelled and repeated. In principle you can get away with a simple atomic reference (that is much faster) when the set of the updated variables is always the same - you make a pair of them and update them together.
- DigitalJack 10y agoInteresting. I've never needed anything other than the occasional atom.
- platz 10y agoIf you're using an atom, you're already sticking state in a place; the problem with atoms is choosing granularity. If one atom has everything in it, it will become a point of contention. If your compare-and-set returns false, what do you do? STM resolves these issues by letting you coordinate updates to more granular, finer-scoped refs
- eveningcoffee 10y agoThe drawback here is that the STM (at least the Clojure implementation few years ago when I tested it) is orders of magnitude slower than simple atomic reference. It actually does not have to be, but this is how it is (was) implemented. I built a simple STM (in Java) as part of my bachelor thesis and its performance was comparable to the atomic reference.
- platz 10y agoIf all you need is a simple atomic reference, then great. I'm not convinced a single atomic reference sufficiently covers enough use cases, however.
- eveningcoffee 10y agoYes, it probably does not, and I think that using STM is actually fine in Clojure. Just you have to pay attention to the limitations.
- chrisseaton 10y ago> but provides little semantic value over compare-and-set! on an atom Can you compare and set on multiple atoms at the same time? If not, how do you make updates atomic without using either STM, a global lock, or re-implementing STM yourself with an incremental locking and rollback system.
- iso-8859-1 10y agoTwo Haskell books have chapters on STM: http://chimera.labs.oreilly.com/books/1230000000929/ch10.html http://chimera.labs.oreilly.com/books/1230000000929/ch10.htm... http://book.realworldhaskell.org/read/software-transactional-memory.html http://book.realworldhaskell.org/read/software-transactional...