开发者

Fair concurrent `map` function in haskell?

开发者 https://www.devze.com 2023-03-04 21:24 出处:网络
Say I am simulating a network of some sort, and have a function which broadcasts a value over a list of Chans:

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
0

精彩评论

暂无评论...
验证码 换一张
取 消