2022-09-30 07:18:15 +03:00
|
|
|
module Main
|
|
|
|
|
|
|
|
import System
|
|
|
|
import System.Concurrency
|
|
|
|
|
|
|
|
producer : Channel Nat -> IO ()
|
|
|
|
producer ch =
|
|
|
|
do send 1
|
|
|
|
send 2
|
|
|
|
send 3
|
|
|
|
send 4
|
2022-10-03 23:25:05 +03:00
|
|
|
where
|
|
|
|
send : Nat -> IO ()
|
|
|
|
send i =
|
|
|
|
do putStrLn $ "> " ++ show i
|
|
|
|
channelPut ch i
|
2022-09-30 07:18:15 +03:00
|
|
|
|
|
|
|
consumer : Channel Nat -> IO ()
|
|
|
|
consumer ch =
|
|
|
|
do recv
|
|
|
|
recv
|
|
|
|
recv
|
|
|
|
recv
|
2022-10-03 23:25:05 +03:00
|
|
|
where
|
|
|
|
recv : IO ()
|
|
|
|
recv =
|
|
|
|
do usleep 100000
|
|
|
|
v <- channelGet ch
|
|
|
|
putStrLn $ "< " ++ show v
|
2022-09-30 07:18:15 +03:00
|
|
|
|
|
|
|
main : IO ()
|
|
|
|
main =
|
|
|
|
do ch <- makeChannel
|
|
|
|
p <- fork $ producer ch
|
|
|
|
c <- fork $ consumer ch
|
|
|
|
threadWait c
|
|
|
|
threadWait p
|
|
|
|
putStrLn "done"
|
|
|
|
|