Broken beyond repair: Control.Concurrent.SampleVar

Felipe Lessa felipe.lessa at gmail.com
Sun Apr 12 14:25:12 EDT 2009


On Sun, Apr 12, 2009 at 05:12:15PM +0100, Chris Kuklewicz wrote:
> Felipe Lessa wrote:
> >> writeSampleVar :: SampleVar a -> a -> IO ()
> >> writeSampleVar s x = block $ withMVar (svLock s) $ const $ do
> >>   tryTakeMVar (svData s)
> >>   putMVar (svData s) x
>
> That is obvious, but 'block $' is not atomic.
>
> Threads 1 and 2 get past the tryTakeMVar.

That's why there is a "withMVar (svLock s)", the "const $ do"
part is executed serially. It is impossible to have two threads
past the "tryTakeMVar".

> Then thread 1 succeeds with putMVar and thread 2 blocks.
>
> Thread 2 is not supposed to block, so this is a failure to agree with the
> specification.

Well, it doesn't block. In the worst case I can imagine a thread
could be killed after the 'tryTakeMVar', leaving the 'SampleVar'
empty, but I guess the 'block' would prevent that.

--
Felipe.


More information about the Libraries mailing list