看wiki,不懂,用lock怎么就不行了?
In 2005, Tim Harris, Simon Marlow, Simon Peyton Jones, and Maurice Herlihy
described an STM system built on Concurrent Haskell that enables arbitrary
atomic operations to be composed into larger atomic operations, a useful
concept impossible with lock-based programming. To quote the authors:
Perhaps the most fundamental objection [...] is that lock-based programs do
not compose: correct fragments may fail when combined. For example, consider
a hash table with thread-safe insert and delete operations. Now suppose
that we want to delete one item A from table t1, and insert it into table t2
; but the intermediate state (in which neither table contains the item) must
not be visible to other threads. Unless the implementor of the hash table
anticipates this need, there is simply no way to satisfy this requirement. [
...] In short, operations that are individually correct (insert, delete)
cannot be composed into larger correct operations.
—Tim Harris et al., "Composable Memory Transactions", Section 2: Background
, pg.2