Newer
Older
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import Control.Monad
import System.Random
import Control.Concurrent
import Control.Concurrent.STM
import Control.Concurrent.STM.TMQueue
import Data.Monoid ((<>))
fuzzDelay = True
fuzz :: IO ()
fuzz = when fuzzDelay $ do
seconds <- getStdRandom random :: IO Double
threadDelay $ round (realToFrac (1000000 * seconds))
printManager :: TMQueue [String] -> IO ()
printManager inputs = loop
where
loop = do
mbThingToShow <- atomically (readTMQueue inputs)
fuzz
case mbThingToShow of
Nothing -> pure ()
Just thing -> do
mapM_ putStrLn thing
fuzz
loop
counterManager :: TMQueue [String] -> TMQueue Int -> IO ()
counterManager printQueue inputs = loop 0
where
loop !i = do -- requires BangPatterns language extension
mint <- atomically (readTMQueue inputs)
case mint of
Nothing -> pure ()
Just int -> do
fuzz
let newCount = int + i
fuzz
atomically (writeTMQueue printQueue ["The count is now " <> show newCount, "---------------"])
fuzz
loop newCount
main :: IO ()
main = do
printQueue <- atomically newTMQueue
counterQueue <- atomically newTMQueue
forkIO $ counterManager printQueue counterQueue
forkIO $ printManager printQueue
fuzz
atomically $ writeTMQueue printQueue ["Starting up"]
fuzz
replicateM_ 10 $ do
atomically $ writeTMQueue counterQueue 1
fuzz
atomically $ writeTMQueue printQueue ["Finishing up"]