Skip to content

Instantly share code, notes, and snippets.

@AlexanderAA
Last active December 27, 2015 15:39
Show Gist options
  • Save AlexanderAA/7349033 to your computer and use it in GitHub Desktop.
Save AlexanderAA/7349033 to your computer and use it in GitHub Desktop.
module Fork (
main
) where
import Control.Concurrent (forkIO, ThreadId, threadDelay)
import Control.Concurrent.STM
import Control.Monad
import Control.Monad.IO.Class (liftIO)
import System.Random
second :: Int
second = 1000000 --microseconds
action :: String -> IO ()
action message = do
delay <- getStdRandom (randomR (0, 5*second))
threadDelay delay
putStrLn $ (show delay) ++ " * " ++ message
z1 messages = forkIO ((void . (mapM (forkIO . action)) . (take 10000)) messages)
z2 = (mapM (forkIO . action)) . (take 10000)
main = do
let messages = map (("M" ++) . show) [1..]
p <- (z2 messages)
print p
--threadDelay (3*second)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment