Skip to content

Instantly share code, notes, and snippets.

@vlastachu
Created February 14, 2016 22:04
Show Gist options
  • Save vlastachu/3d37fc4478d3786e0761 to your computer and use it in GitHub Desktop.
Save vlastachu/3d37fc4478d3786e0761 to your computer and use it in GitHub Desktop.
module Main (main) where
import Control.Concurrent (forkIO, threadDelay)
import Control.Concurrent.Chan (Chan, newChan, writeChan, readChan)
import Control.Monad (forM_, replicateM)
baseDelay = 2000
writerDelay = 1 * baseDelay
readerDelay = 2 * baseDelay
writer :: Chan Int -> IO ()
writer c = forM_ [1..11] $ \i -> do
threadDelay writerDelay
writeChan c i
putStrLn ("writer iteration: " ++ show i)
reader :: Chan Int -> IO ()
reader c = forM_ [1..10] $ \i -> do
threadDelay readerDelay
chanRes <- readChan c
putStrLn ("reader iteration: " ++ show i ++ "; channel: " ++ show chanRes)
main =
do
c <- newChan
_ <- forkIO $ writer c
_ <- forkIO $ reader c
threadDelay $ 30 * baseDelay
result <- readChan c
putStrLn (show result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment