How do you implement Software Transactional Memory?

后端 未结 5 1867
渐次进展
渐次进展 2021-02-01 18:49

In terms of actual low level atomic instructions and memory fences (I assume they\'re used), how do you implement STM?

The part that\'s mysterious to me is that given s

5条回答
  •  孤城傲影
    2021-02-01 18:59

    I suggest you watch this presentation: http://www.infoq.com/presentations/Value-Identity-State-Rich-Hickey

    In the second half it explains how to update values without leaving them in an undefined state. For example - if you have a tree that you want to update in STM-style you don't change the previous version at all. Let's say that tree is a pointer to the root of the tree. The only thing you create is the nodes that changed (but they can refer to nodes in the original snapshot of the tree.

    Then you do a compare-and-swap on the tree pointer. If it succeeded, then everyone will now see your new tree and the old one can be garbage-collected. If it hasn't, then you repeat the process and the tree you just constructed is garbage collected.

    The big idea is that you don't need to detect if anyone else changed the tree until you actually swap the new and old values, so there are no "conflicts" or "clobbered values" from the typical multithreaded programming.

提交回复
热议问题