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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Isaac Dupree
Peter Verswyvelen wrote:
> ... by spawning and killing two threads (which might be an expensive operation, I'm not sure)

pretty cheap in GHC -- they're not system threads

> Am I wrong in this? If so, is this something that might be considered as a future enhancement in the GHC libraries and runtime?

Also, look at STM (e.g. TVars), which is designed to do what
you want more directly, I think.

(timeouts might still use the GHC-thread thing.  Don't worry
about its performance unless you're measurably suffering
from it...)

-Isaac
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

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

Ryan Ingram
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

  -- ryan

On Wed, Nov 26, 2008 at 11:41 AM, Isaac Dupree <[hidden email]> wrote:

> Peter Verswyvelen wrote:
>>
>> ... by spawning and killing two threads (which might be an expensive
>> operation, I'm not sure)
>
> pretty cheap in GHC -- they're not system threads
>
>> Am I wrong in this? If so, is this something that might be considered as a
>> future enhancement in the GHC libraries and runtime?
>
> Also, look at STM (e.g. TVars), which is designed to do what you want more
> directly, I think.
>
> (timeouts might still use the GHC-thread thing.  Don't worry about its
> performance unless you're measurably suffering from it...)
>
> -Isaac
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

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

Duncan Coutts
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

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe