Say I am simulating a network of some sort, and have a function which broadcasts a value over a list of Chans:
broadcast :: a -> [Rec开发者_高级运维eiver] -> IO ()
broadcast a = mapM_ (send a)
This will "send" the data to each Receiver in order. I would like instead to broadcast nondeterministically to all of the Receivers.
Thanks for any clues.
How about just using GHC's concurrency?
broadcast :: a -> [Receiver] -> IO ()
broadcast a = mapM_ (forkIO . send a)
Then make sure to use the threaded RTS (compile with ghc -threaded
).
http://hackage.haskell.org/package/random-shuffle-0.0.2
broadcast a rs = do
g <- newStdGen
mapM_ (send a) $ shuffle' rs (length rs) g
精彩评论