Question about withMVar

The documentation of withMVar (in Control.Concurrent.MVar module of base package) says: {-| 'withMVar' is an exception-safe wrapper for operating on the contents of an 'MVar'. This operation is exception-safe: it will replace the original contents of the 'MVar' if an exception is raised (see "Control.Exception"). However, it is only atomic if there are no other producers for this 'MVar'.-}withMVar :: MVar a -> (a -> IO b) -> IO bwithMVar m io = mask $ \restore -> do a <- takeMVar m b <- restore (io a) `onException` putMVar m a putMVar m a return b Can someone shed some light on what is meant by the statement - "However, it is only atomic if there are no other producers for this 'MVar'."? I hope this is the right mailing list for this question. Thanks, Harendra

It's pointing out that it's possible that after it takes the MVar some
other thread could put the MVar. That could cause it to block, or could
violate assumptions about how the MVar value changes. The basic rule is
that whenever you're using an MVar you need all the threads to agree to
some particular discipline. Most commonly, and best supported by the API,
is "take, then put". However, there are other patterns, the most important
of which is probably "put once", used to communicate thread completion.
On Sun, Jul 18, 2021, 9:49 AM Harendra Kumar
The documentation of withMVar (in Control.Concurrent.MVar module of base package) says:
{-| 'withMVar' is an exception-safe wrapper for operating on the contents of an 'MVar'. This operation is exception-safe: it will replace the original contents of the 'MVar' if an exception is raised (see "Control.Exception"). However, it is only atomic if there are no other producers for this 'MVar'.-}withMVar :: MVar a -> (a -> IO b) -> IO bwithMVar m io = mask $ \restore -> do a <- takeMVar m b <- restore (io a) `onException` putMVar m a putMVar m a return b
Can someone shed some light on what is meant by the statement - "However, it is only atomic if there are no other producers for this 'MVar'."?
I hope this is the right mailing list for this question.
Thanks, Harendra
_______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

This recent commit attempted to clarify the situation.
https://gitlab.haskell.org/ghc/ghc/-/commit/bb8e0df8f4187a4f4d0788dd3da3ef6f...
Does that help?
On Sun, Jul 18, 2021 at 2:49 PM Harendra Kumar
The documentation of withMVar (in Control.Concurrent.MVar module of base package) says:
{-| 'withMVar' is an exception-safe wrapper for operating on the contents of an 'MVar'. This operation is exception-safe: it will replace the original contents of the 'MVar' if an exception is raised (see "Control.Exception"). However, it is only atomic if there are no other producers for this 'MVar'. -} withMVar :: MVar a -> (a -> IO b) -> IO b withMVar m io = mask $ \restore -> do a <- takeMVar m b <- restore (io a) `onException` putMVar m a putMVar m a return b
Can someone shed some light on what is meant by the statement - "However, it is only atomic if there are no other producers for this 'MVar'."?
I hope this is the right mailing list for this question.
Thanks, Harendra
_______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

The action is atomic as long as while one thread is using withMVar another
thread isn't using putMVar
On Sun, Jul 18, 2021 at 6:49 AM Harendra Kumar
The documentation of withMVar (in Control.Concurrent.MVar module of base package) says:
{-| 'withMVar' is an exception-safe wrapper for operating on the contents of an 'MVar'. This operation is exception-safe: it will replace the original contents of the 'MVar' if an exception is raised (see "Control.Exception"). However, it is only atomic if there are no other producers for this 'MVar'.-}withMVar :: MVar a -> (a -> IO b) -> IO bwithMVar m io = mask $ \restore -> do a <- takeMVar m b <- restore (io a) `onException` putMVar m a putMVar m a return b
Can someone shed some light on what is meant by the statement - "However, it is only atomic if there are no other producers for this 'MVar'."?
I hope this is the right mailing list for this question.
Thanks, Harendra
_______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
-- Eric Mertens
participants (4)
-
David Feuer
-
Eric Mertens
-
Harendra Kumar
-
Matthew Pickering