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 only lock the nodes that you are actually modifying. STM will easily beat a solution that uses one global lock per whole tree.
However, as I understand it, this behavior is not automatic, is it? If I use a TVar (Map k a)
, will it not act as a single global lock on the whole map? And to gain the benefit of this fine-grained behavior, I (or someone) would have to implement a map replacement (TMap
, for example) that contains TVars
internally, correct?
This might seem like an obvious question, but reading up on STM implementation I was confused between reads of TVar
s and reads of memory locations. I just want to make sure I have it right!
Bartosz goes further to say:
Per-node manual locking is hard to implement correctly because of the risk of deadlocks.
The difference with STM, as I understand it, is that while the STM implementation in effect uses locks the way a manually-locked solution might, the actual acquiry and release of locks is handled by the runtime, not the programmer - correct?
TVar
s and not aTVar (Map k v)
. – GoldsberryMap
, or would I need to completely re-implementMap
to useTVar
s internally? – HalcombTVar
s. More specifically,Map k (TVar v)
. – Goldsberry