Skip to content

Instantly share code, notes, and snippets.

@DeTeam
Created August 25, 2012 19:32
  • Star 0 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 DeTeam/3469872 to your computer and use it in GitHub Desktop.
import Control.Concurrent
import Control.Concurrent.Chan
import System.Random
delayedOutput :: String -> Chan String -> IO ()
delayedOutput m c = do
gen <- newStdGen
let (t, _) = randomR (1, 10) gen
-- delay in microseconds
threadDelay (t * 1000000)
-- display message and leave then
putStrLn m
writeChan c "done"
return ()
main :: IO ()
main = do
-- channel for communication, waiting
c <- newChan
putStrLn "Fight!"
forkIO $ delayedOutput "Finish him!" c
-- here we're blocked for thread to finish
readChan c
return ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment