|Maintainer||Luke Palmer <email@example.com>|
An IVar is an write-once variable (the name comes from "immutable variable"). In addition to encapsulating this common idiom, it also provides a way to block on multiple variables simultaneously, resuming when the first of them is written.
This module is careful not to create memory leaks, and prefers to maintain good long-term memory performance than to be super-fast. It should be reasonably fast though.
This module is designed to be imported qualified, as in:
import qualified Data.IVar as IVar
import qualified Data.IVar as IVar import Control.Concurrent main = do iv <- IVar.new iv' <- IVar.new forkIO $ threadDelay 10000000 >> IVar.write iv' "my spoon is too big" let merger = IVar.read iv `mplus` IVar.read iv' print =<< IVar.nonblocking merger -- most likely "Nothing" print =<< IVar.blocking merger -- waits a while, then prints IVar.write iv' "i am a banana" -- throws error "IVar written twice"
Write a value to an IVar. If the IVar already has a value, throws an error "Attempt to write to an IVar twice".
Reader is a functor (also monad) for reading IVars. This provides composability when blocking on the first of a set of IVars, as you can block on several IVars of different types.
The MonadPlus and Monoid instances for Reader are equivalent. It tries the left action ; if it blocks, then it tries the right action ; if it blocks, then the whole action blocks until one of the two is available.
Run a reader nonblocking. Returns
Just x if a value