O'Reilly logo

Haskell Cookbook by Yogesh Sajanikar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How it works...

  1. Create a new project called working-with-STM with the simple stack template:
        stack new working-with-STM simple
  1. 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
          ghc-options:         -threaded
          default-language:    Haskell2010
          build-depends:       base >= 4.7 && < 5
                             , stm
  1. Open src/Main.hs. We will be adding our source here. Import Control.Concurrent.STM to importing STM module:
        module Main where

        import Control.Concurrent.STM
        import Control.Concurrent
  1. Define the account type which points to TVar Int, which represents the current balance:
        newtype Account = Account (TVar Int)
  1. Define ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required