summaryrefslogtreecommitdiff
path: root/testsuite/tests/concurrent/should_run/conc003.hs
blob: c7b1f9a56c2975f4146960c9dd7913c575db4dec (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"