[Haskell-cafe] What does unpacking an MVar really mean?

Ryan Ingram ryani.spam at gmail.com
Tue Jul 31 07:04:44 CEST 2012


I'm not sure I totally understand your question about 'unpacking' an MVar,
but I'm going to assume you mean data structures that use the {-# UNPACK
#-} pragma, like in Control.Concurrent.Future [1] and
Control.Concurrent.NamedLock [2].

Here is how MVar is defined in GHC [3]:
    data MVar a = MVar (MVar# RealWorld a)

A "MVar# s a" is an unboxed pointer to a structure understood by the GHC
runtime.

So yes, you can imagine a MVar as a pointer-to-pointer.  The structure it
points at likely has another pointer to the embedded boxed "a", so it may
even be pointer-to-pointer-to-pointer.

The MVar data structure exists to allow laziness; for example

   let x = unsafePerformIO (newMVar ()) in ()

is likely to not allocate an MVar#, just a thunk that would create an MVar
if it was evaluated.  Unboxed objects (represented by convetion with # in
GHC), on the other hand, are strict, so if you have an MVar# RealWorld (),
you know it points to a valid MVar#.



More information about the Haskell-Cafe mailing list