Skip to content

Instantly share code, notes, and snippets.

@wilkes
Created July 29, 2009 19:02
Show Gist options
  • Save wilkes/158339 to your computer and use it in GitHub Desktop.
Save wilkes/158339 to your computer and use it in GitHub Desktop.
orderedForkIO :: [IO a] -> IO [a]
orderedForkIO actions = do
-- create a blocking container to hold the results for each IO action
mvars <- replicateM (length actions) newEmptyMVar
-- fork and run the action then put the result in the container
let run mvar action = forkIO $ action >>= putMVar mvar
-- fork and run each action with a container in the local run function
forkIO $ zipWithM_ run mvars actions
-- collect all the results, blocking until they are finished
mapM takeMVar mvars
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment