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-STM
with thesimple
stack template:
stack new working-with-STM simple
- Add the
ghc-options
subsection to theexecutable
section. Set the option to-threaded
. Also addstm
to 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.STM
to importing STM...