Skip to content

Instantly share code, notes, and snippets.

@AlexanderAA
Created November 7, 2013 05:21
Show Gist options
  • Save AlexanderAA/7349395 to your computer and use it in GitHub Desktop.
Save AlexanderAA/7349395 to your computer and use it in GitHub Desktop.
module Fork (
main
) where
import Control.Concurrent (forkIO, forkOS, ThreadId, threadDelay)
import Control.Monad (mapM, void)
import System.Random (getStdRandom, randomR)
second :: Int
second = 1000000 --microseconds
action :: String -> IO ()
action message = do
delay <- getStdRandom (randomR (0, 5*second))
threadDelay delay
putStrLn $ (show delay) ++ " * " ++ message
main :: IO ()
main = do
let messages = map (("M" ++) . show) [1..]
let actions = map (forkOS . action) messages
let workUnit = take 10 actions
result <- sequence workUnit
print result
--threadDelay (10*second)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment