[Haskell-cafe] Re: [Haskell] Wait for *either* MVar to be set

Duncan Coutts duncan.coutts at worc.ox.ac.uk
Wed Nov 26 17:17:05 EST 2008


On Wed, 2008-11-26 at 13:25 -0800, Ryan Ingram wrote:
> In fact:
> 
> > import Control.Concurrent.STM
> > import Control.Concurrent.STM.TMVar
> 
> > -- gets a value from one of a list of TMVars
> > takeTMVars :: [TMVar a] -> STM (TMVar a, a)
> > takeTMVars = foldr fetch retry where
> >    fetch v act = (takeTMVar v >>= \a -> return (v, a)) `orElse` act
> 
> > -- puts the given value into exactly one of a list of TMVars
> > putTMVars :: [TMVar a] -> a -> STM (TMVar a)
> > putTMVars vs a = foldr put retry vs where
> >    put v act = (putTMVar v a >> return v) `orElse` act

Of course those are TMVars, using STM. So in STM it is easy, whereas
with MVars it is a bit harder.

Duncan



More information about the Haskell-Cafe mailing list