Simon Marlow wrote:
> Let me see if I've got the semantics right: takeMVarMulti makes a
> non-deterministic choice between the full MVars to return the value, and if
> there are no full MVars it waits for the first one to become full?
Yes that's precisely right.  putMVarMulti is the converse.  (So it only
writes to one location, which will confuse some people.)
> 
> Defining this in Haskell is pretty hard.  I managed to do it for two MVars
> (code at the end of this message).  There might be an easier way to do it
> using special code in the scheduler, but I need to think about that some
> more.
It's _because_ defining it in Haskell is pretty hard that I was thinking maybe
it should be added to the concurrency primitives.  Unless I'm missing something,
with the current concurrency primitives you cannot make a non-deterministic
choice between N MVars with less than N threads, unless you do it by polling
at intervals

Actually even takeMVarMulti and putMVarMulti could be generalised.  A more
general non-deterministic MVar operation function might be

multi :: [Multi a] -> IO a

data Multi a =
   forall b . PutMVar (MVar b) b IO a
   forall b . TakeMVar (MVar b) (b -> IO a)
This would wait for the first doable PutMVar or TakeMVar and then
execute the corresponding continuation.

Reply via email to