[Haskell-cafe] Question about takeMVar

Matthew Brecknell haskell at brecknell.org
Wed Nov 28 21:32:00 EST 2007

> 'takeMVar' documentation says "if there are
> multiple threads blocked in takeMVar, and the
> MVar becomes full, only one thread will be
> woken up."
> Since 'takeMVar' is a reading function, i.e.,
> it doesn't change the value of the
> "variable", why waking up only one thread? If
> we wake multiple threads, there's no risk of
> one changing the value while the other is
> reading.

Also in the docs: After a takeMVar, the MVar is left empty.

The behaviour you describe is more like readMVar. Because readMVar does
a takeMVar followed by a putMVar, it will wake up multiple threads. If
you want to broadcast to multiple threads, you might also want to
consider dupChan in Control.Concurrent.Chan.

More information about the Haskell-Cafe mailing list