[Haskell] Optimizing locking with MVars

John Meacham john at repetae.net
Tue May 2 18:37:03 EDT 2006

On Tue, May 02, 2006 at 12:15:20PM +0400, Bulat Ziganshin wrote:
> Second, i've developed my own simplified version of this procedure.
> Here i should say that my library uses "MVar ()" field to hold lock
> and separate immutable data field with actual data locked:
> data WithLocking h = WithLocking h !(MVar ())

This reminds me, I wonder if we should have an MVar varient that is
_just_ for locking, it would have no separate take and put primitives,
just a withLock, enforcing the restriction that the thread that took the
lock is the same one that will return it.

this would allow standard priority inversion reduction techniques to be
used and could signifigantly mitigate the problem of accidentally
performing a long pure evaluation while holding a lock. when another
thread comes along that needs a lock, it will give its CPU time to the
thread holding it so it can finish up its long computation.

with MVars, you never know whether an MVar is being used for simple
locking or as an inter-thread communication mechanism.

John Meacham - ⑆repetae.net⑆john⑈

More information about the Haskell mailing list