[Haskell-cafe] MVar which can not be null ?

Edward Z. Yang ezyang at MIT.EDU
Mon Mar 18 09:15:30 CET 2013


If you are doing IO operations, then the operation is hardly atomic, is it?

Just take from the MVar, compute, and when you're done, put a value
back on the MVar.  So long as you can guarantee all users of the MVar
take before putting, you will have the desired semantics.

Something worth considering: what are the desired semantics if an
asynchronous exception is thrown on the thread servicing the MVar?
If the answer is to just quit, what if it has already performed
externally visible IO actions?  If the answer is to ignore it, what
if the thread gets wedged?

Edward

Excerpts from s9gf4ult's message of Mon Mar 18 01:07:42 -0700 2013:
> 18.03.2013 13:26, Alexander V Vershilov ?????:
> 
> I can not use atomicModifyIORef because it works with pure computation
> 
> atomicModifyIORef :: IORef
> <http://hackage.haskell.org/packages/archive/base/latest/doc/html/Data-IORef.html#t:IORef>
> a -> (a -> (a, b)) -> IO
> <http://hackage.haskell.org/packages/archive/base/latest/doc/html/System-IO.html#t:IO>
> b
> 
> nor STM, becuase IO is not acceptable inside STM transaction.
> 
> I just need some thread-safe blocking variable like MVar
> 
> modifyMVar :: MVar
> <http://hackage.haskell.org/packages/archive/base/4.6.0.1/doc/html/Control-Concurrent-MVar.html#t:MVar>
> a -> (a -> IO
> <http://hackage.haskell.org/packages/archive/base/4.6.0.1/doc/html/System-IO.html#t:IO>
> (a, b)) -> IO
> <http://hackage.haskell.org/packages/archive/base/4.6.0.1/doc/html/System-IO.html#t:IO>
> b



More information about the Haskell-Cafe mailing list