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.
Working with STM
How it works...
- Create a new project called working-with-STM with the simple stack template:
stack new working-with-STM simple
- Add the ghc-options subsection to the executable section. Set the option to -threaded. Also add stm to the build-depends subsection:
executable working-with-STM hs-source-dirs: src main-is: Main.hs ...