Skip to content

Instantly share code, notes, and snippets.

@kazu-yamamoto
Last active October 7, 2017 11:30
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save kazu-yamamoto/cde22cf569e7e78a179d30a9982b1433 to your computer and use it in GitHub Desktop.
Concurrent hello world in Haskell
module Main (main) where
import Control.Concurrent
main :: IO ()
main = do
codechan <- newEmptyMVar
wait <- newEmptyMVar
myid <- myThreadId
putStrLn $ "I'm " ++ show myid
cid <- forkIO $ child codechan wait
let code = putStrLn ("Hello, " ++ show cid ++ "!")
putMVar codechan code
takeMVar wait
child :: MVar (IO ()) -> MVar () -> IO ()
child codechan wait = do
myid <- myThreadId
putStrLn $ "I'm " ++ show myid
code <- takeMVar codechan
code
putMVar wait ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment