Working with STM
In this recipe, we will work with STM (Software Transactional Memory) which provides atomic blocks for executions. It provide more guarantees the about atomicity of the operation than MVars. We will work with an example of a bank account, where simultaneous transactions are trying to do the transaction with the same bank account.
How it works...
- Create a new project called
working-with-STMwith thesimplestack template:
stack new working-with-STM simple
- Add the
ghc-optionssubsection to theexecutablesection. Set the option to-threaded. Also addstmto the build-depends subsection:
executable working-with-STM
hs-source-dirs: src
main-is: Main.hs
ghc-options: -threaded
default-language: Haskell2010
build-depends: base >= 4.7 && < 5
, stm- Open
src/Main.hs. We will be adding our source here. ImportControl.Concurrent.STMto importing STM...