HORIZON HASKELLDocslts/ghc-10.0.xa8896112026-10-18Search names, modules, packages, or :: a typeCtrl K

GHC 10.0.0.20260917 · lts/ghc-10.0.x · a889611 · 2026-10-18

Moduleconcurrency-1.11.0.3Haskell2010

Control.Concurrent.Classy.MVar

An MVar t is mutable location that is either empty or contains a value of type t. It has two fundamental operations: putMVar which fills an MVar if it is empty and blocks otherwise, and takeMVar which empties an MVar if it is full and blocks otherwise. They can be used in multiple different ways:

  1. As synchronized mutable variables,

  2. As channels, with takeMVar and putMVar as receive and send, and

  3. As a binary semaphore MVar (), with takeMVar and putMVar as wait and signal.

Deviations: There is no Eq instance for MonadConc the MVar type. Furthermore, the mkWeakMVar and addMVarFinalizer functions are not provided. Finally, normal MVars have a fairness guarantee, which dejafu does not currently make use of when generating schedules to test, so your program may be tested with unfair schedules.

  • 10 values

MVars

18 declarations
familytype family MVar (m :: Type -> Type) :: Type -> Type
#

The mutable reference type, like MVars. This may contain one value at a time, attempting to read or take from an "empty" MVar will block until it is full, and attempting to put to a "full" MVar will block until it is empty.

Instances10MVar, …
  • type MVar IO = MVarDefined in concurrency-1.11.0.3 · Control.Monad.Conc.Class
  • type MVar (IsConc m) = MVar mDefined in concurrency-1.11.0.3 · Control.Monad.Conc.Class
  • type MVar (IdentityT m) = MVar mDefined in concurrency-1.11.0.3 · Control.Monad.Conc.Class
  • type MVar (RWST r w s m) = MVar mDefined in concurrency-1.11.0.3 · Control.Monad.Conc.Class
  • type MVar (RWST r w s m) = MVar mDefined in concurrency-1.11.0.3 · Control.Monad.Conc.Class
  • type MVar (ReaderT r m) = MVar mDefined in concurrency-1.11.0.3 · Control.Monad.Conc.Class
  • type MVar (StateT s m) = MVar mDefined in concurrency-1.11.0.3 · Control.Monad.Conc.Class
  • type MVar (StateT s m) = MVar mDefined in concurrency-1.11.0.3 · Control.Monad.Conc.Class
  • type MVar (WriterT w m) = MVar mDefined in concurrency-1.11.0.3 · Control.Monad.Conc.Class
  • type MVar (WriterT w m) = MVar mDefined in concurrency-1.11.0.3 · Control.Monad.Conc.Class
methodnewEmptyMVar :: m (MVar m a)
#

Create a new empty MVar.

newEmptyMVar = newEmptyMVarN ""
methodnewEmptyMVarN :: String -> m (MVar m a)
#

Create a new empty MVar, but it is given a name which may be used to present more useful debugging information.

newEmptyMVarN _ = newEmptyMVar
valuenewMVarN :: MonadConc m => String -> a -> m (MVar m a)
#

Create a new MVar containing a value, but it is given a name which may be used to present more useful debugging information.

methodtakeMVar :: MVar m a -> m a
#

Take a value from a MVar. This "empties" the MVar, allowing a new value to be put in. This will block if there is no value in the MVar already, until one has been put.

methodputMVar :: MVar m a -> a -> m ()
#

Put a value into a MVar. If there is already a value there, this will block until that value has been taken, at which point the value will be stored.

methodreadMVar :: MVar m a -> m a
#

Block until a value is present in the MVar, and then return it. This does not "remove" the value, multiple reads are possible.

valueswapMVar :: MonadConc m => MVar m a -> a -> m a
#

Swap the contents of a MVar, and return the value taken. This function is atomic only if there are no other producers fro this MVar.

methodtryTakeMVar :: MVar m a -> m (Maybe a)
#

Attempt to take a value from a MVar non-blockingly, returning a Just (and emptying the MVar) if there was something there, otherwise returning Nothing.

methodtryPutMVar :: MVar m a -> a -> m Bool
#

Attempt to put a value in a MVar non-blockingly, returning True (and filling the MVar) if there was nothing there, otherwise returning False.

valuewithMVar :: MonadConc m => MVar m a -> (a -> m b) -> m b
#

Operate on the contents of a MVar, replacing the contents after finishing. This operation is exception-safe: it will replace the original contents of the MVar if an exception is raised. However, it is only atomic if there are no other producers for this MVar.

valuemodifyMVar_ :: MonadConc m => MVar m a -> (a -> m a) -> m ()
#

An exception-safe wrapper for modifying the contents of a MVar. Like withMVar, modifyMVar will replace the original contents of the MVar if an exception is raised during the operation. This function is only atomic if there are no other producers for this MVar.

valuemodifyMVar :: MonadConc m => MVar m a -> (a -> m (a, b)) -> m b
#

A slight variation on modifyMVar_ that allows a value to be returned (b) in addition to the modified value of the MVar.