atomic MVar overwrite (was RE: [Haskell-cafe] How to use QSem?)

Simon Marlow simonmar at microsoft.com
Thu Jun 24 05:08:03 EDT 2004


On 23 June 2004 17:46, S. Alexander Jacobson wrote:

> Basically, what I want is a tryReadSampleVar
> function.
> 
> Or, if we stick with MVars, I don't want another
> thread to operate between the tryTakeMVar and the
> putMVar.  In particular, I don't want another
> thread to believe (mistakenly) that the mvar is
> empty when it is simply being updated.
> 
> I suppose I could define a new abstraction where I
> have a second mvar that acts as an exclusive lock
> on the the first MVar.  e.g.
> 
>    data XMVar a = XMVar (MVar ()) MVar a
> 
>    overWriteXMVar (XMVar lock mvar) val =
> 	withMVar lock (\_->tryTakeMVar mvar >> putMVar mvar val)
>    tryTakeXMVar (XMVar lock mvar) val =
> 	withMVar lock (\_->tryTakeMVar mvar)
> 
> But, I think it would be more sane simply to
> have a tryReadSampleVar somewhere.

If you send me code for tryReadSampleVar, I'll incorporate it.
Unfortunately SampleVar is abstract, so you'll need to take the source
for that library (libraries/base/Control/Concurrent/SampleVar.hs in a
GHC distribution) and modify it to create your own version.

Cheers,
	Simon


More information about the Haskell-Cafe mailing list