HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduleprimitive-0.9.1.0Haskell2010

Data.Primitive.MutVar

Primitive boxed mutable variables. This is a generalization of Data.IORef, Data.STRef and Data.STRef.Lazy to work in any PrimMonad.

  • 1 type
  • 11 values
  • Packageprimitive-0.9.1.0
  • Exports12
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceMutVar.hs
datadata MutVar s a
#

A MutVar behaves like a single-element mutable array associated with a primitive state token.

Constructors

Instances1Eq
  • Eq (MutVar s a)Defined in primitive-0.9.1.0 · Data.Primitive.MutVar

Modify

4 declarations
valueatomicModifyMutVar
  1. :: PrimMonad m
  2. => MutVar (PrimState m) a
  3. -> a -> (a, b)
  4. -> m b
#

Atomically mutate the contents of a MutVar.

This function is useful for using MutVar in a safe way in a multithreaded program. If you only have one MutVar, then using atomicModifyMutVar to access and modify it will prevent race conditions.

Extending the atomicity to multiple MutVars is problematic, so if you need to do anything more complicated, using MVar instead is a good idea.

atomicModifyMutVar does not apply the function strictly. This means if a program calls atomicModifyMutVar many times, but seldom uses the value, thunks will pile up in memory resulting in a space leak. To avoid this problem, use atomicModifyMutVar' instead.

Interop with STRef and IORef

4 declarations