Copyright | (c) Dong Han 2017-2020 |
---|---|
License | BSD |
Maintainer | winterland1989@gmail.com |
Stability | experimental |
Portability | non-portable |
Safe Haskell | None |
Language | Haskell2010 |
This module provides some concurrent BIO
node, to ease the implementation of producer-consumer model.
All sources and sinks return by this module are safe to be used in multiple threads.
- Use
newTQueueNode
for common cases. - Use
newTBQueueNode
if you have a fast producer and you don't want input get piled up in memory. - Use
newBroadcastTChanNode
if you want messages get broadcasted, i.e. every message written by producers will be received by every consumers.
It's important to correctly set the numebr of producers, internally it keeps a counter on how many producers reached their ends, and send EOF to all consumers when last producer ends. So it's a good idea to catch exceptions and pull the sink(which indicate EOF) on producer side.
(sink, src) <- newTQueueNode 2 -- it's important to correctly set the numebr of producers
forkIO $ do
...
push x sink -- producer using push
...
pull sink -- when EOF is reached, manually pull, you may consider put it in a bracket.
forkIO $ do
...
(runBIO $ ... >|> sink) -- producer using BIO
onException
(pull sink)
forkIO $ do
...
r <- pull src -- consumer using pull
case r of Just r' -> ...
_ -> ... -- Nothing indicate all producers reached EOF
forkIO $ do
...
runBIO $ src >|> ... -- consumer using BIO
Documentation
Make an unbounded queue and a pair of sink and souce connected to it.
Make an bounded queue and a pair of sink and souce connected to it.