Software Transactional Memory
Software Transactional Memory (STM) is the highest-level general concurrency abstraction we will consider. STM provides composable atomic transactions, meaning we can combine reads, writes, and other operations in multiple memory locations into single atomic operations. Transactions can be aborted or retried.
An STM transaction lives in the STM
monad:
data STM a instance Monad STM
Transactions are performed with the atomically
function:
atomically :: STM a → IO a
Another important primitive is the retry
function, which aborts the current transaction and retries it when some of its dependencies have changed (in some other transaction in another thread):
retry :: STM a
Basic transactional variables are provided by the STM
package itself. Advanced structures are provided by additional packages. The following are provided in stm:Control.Concurrent.STM
:
TVar
: A shared memory location analogous toIORef
, but transactionalTMVar
: Mutable variable analogous toIORef
TChan
: Channels...