Re: [Haskell-cafe] readMVar and the devils

at a guess the magic take put is: block ( do a <- takeMVar x putMVar x a ) return a Keean.

at a guess the magic take put is:
block ( do a <- takeMVar x putMVar x a ) return a
This doesn't prevent the race condition Conor mentioned. It only prevents the thread executing the above code from being interrupted by an asynchronous exception (i.e., Control-C, or another thread killing this one). The only way to prevents race conditions is through discipline (take then put is one example). A good way to enforce this discipline is to use withX-style combinators, and treat the put/take group of operations as primitives. A

On Friday 02 July 2004 15:59, you wrote:
at a guess the magic take put is:
block ( do a <- takeMVar x putMVar x a ) return a
This doesn't prevent the race condition Conor mentioned. It only prevents the thread executing the above code from being interrupted by an asynchronous exception (i.e., Control-C, or another thread killing this one). The only way to prevents race conditions is through discipline (take then put is one example).
True, but it's not the whole thruth. <rant> Is the next thing i'm going to hear "Haskell is a language for the mature programmer" (~= what people who don't know better use to say about C)? The great Pure Functional Language Haskell with the dead-safe static type system (including all those sexy types) gives the programmer nothing more to cope with concurrency than low-level MVars together with imperative style programming.(?!?) </rant> Seriously: What the original poster needs is not discipline but a suitable abstraction built on top of MVar. Like: module MEVar ( MEVar, -- abstract readMEVar, ... ) where -- The 'E' in MEVar stands for 'exclusive read' -- There are probably better names for this import Control.Concurrent newtype MEVar a = MVar (MVar a) readMEVar :: MEVar a -> IO a readMEVar outer = do inner <- takeMVar outer value <- takeMVar inner putMVar inner value putMVar outer inner return value ... Ben
participants (3)
-
Andy Moran
-
Benjamin Franksen
-
MR K P SCHUPKE