[Haskell] Waiting on multiple Chan-nels
Brandon Moore
brandonm at yahoo-inc.com
Tue Dec 12 15:04:20 EST 2006
Setzer, Sebastian (ext) wrote:
> Hi,
> How do you wait on multiple channels, but read only from one of them
> (the first that gets an entry)? Is there a library-function I missed
> which already does this?
>
This is pretty hard with Control.Concurrent.Chan. They are simply not
very easy to compose. STM is designed to be very easy to compose.
(The first word of the title of the first paper - "Composable memory
transactions",
http://research.microsoft.com/%7Esimonpj/papers/stm/index.htm)
If you can switch to TChan, you can use this function:
waitAny :: [TChan a] -> STM a
waitAny chans = msum (map readTChan chans)
which needs
import Control.Concurrent.STM.TChan (TChan, readTChan)
import Control.Monad (msum)
import Control.Monad.STM (STM) --also supplies instance MonadPlus STM
Brandon
More information about the Haskell
mailing list