Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Run a list of IO actions simultaneously, then wait for them all to finish, returning a list of their results.
import Control.Concurrent
import Random
main = print =<< threadAndJoin (replicate 10 randomAction)
randomAction = do
x <- randomRIO (0::Double,2)
threadDelay $ floor $ x * (10 ** 6)
print x >> return x
threadAndJoin :: [IO a] -> IO [a]
threadAndJoin = (>>= mapM takeMVar) . mapM bg
where
bg :: IO a -> IO (MVar a)
bg action = do
x <- newEmptyMVar
forkIO $ action >>= putMVar x
return x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.