summaryrefslogtreecommitdiff
path: root/testsuite/tests/concurrent/should_run/conc003.hs
blob: 253d44dfc8ecd0cb36e0abf9eefee515597c3508 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
module Main where

import Control.Concurrent

-- simple handshaking using two MVars,
-- must context switch twice for each character.

main = do
  ready <- newEmptyMVar
  datum <- newEmptyMVar
  let
    reader = do
        putMVar ready ()
        char <- takeMVar datum
        if (char == '\n')
                then return ()
                else do putChar char; reader

    writer "" = do
        takeMVar ready
        putMVar datum '\n'
    writer (c:cs) = do
        takeMVar ready
        putMVar datum c
        writer cs

  forkIO reader
  writer "Hello World"