How to do it...

  1. Create a new project called working-with-mvar with the simple stack template:
        stack new working-with-mvar simple
  1. Add dependencies on the containers and random libraries in the build-depends sub-section of the executable section. Also add the -threaded option to the ghc-options subsection:
        executable working-with-mvar
          hs-source-dirs:      src
          main-is:             Main.hs
          ghc-options:         -threaded
          default-language:    Haskell2010
          build-depends:       base >= 4.7 && < 5
                             , containers                             , random
  1. Open src/Main.hs. We will be adding our source here. Define the main module, and import headers for using MVar and Chan:
        module Main where

        import Control.Concurrent
        import Control.Concurrent.Chan
        import Data.Map
        import Control.Monad
        import System.Random ...

Get Haskell Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.