2 % (c) The GRASP/AQUA Project, Glasgow University, 1995
4 \section[ChannelVar]{Channel variables}
6 Channel variables, are one-element channels described in the Concurrent
7 Haskell paper (available from @ftp://ftp.dcs.gla.ac.uk/pub/glasgow-fp/drafts@)
14 newCVar, --:: IO (CVar a)
15 putCVar, --:: CVar a -> a -> IO ()
16 getCVar, --:: CVar a -> IO a
24 @MVars@ provide the basic mechanisms for synchronising access to a shared
25 resource. @CVars@, or channel variables, provide an abstraction that guarantee
26 that the producer is not allowed to run riot, but enforces the interleaved
27 access to the channel variable,i.e., a producer is forced to wait up for
28 a consumer to remove the previous value before it can deposit a new one in the @CVar@.
33 = CVar (MVar a) -- prod -> cons
34 (MVar ()) -- cons -> prod
36 newCVar :: IO (CVar a)
37 putCVar :: CVar a -> a -> IO ()
38 getCVar :: CVar a -> IO a
41 = newEmptyMVar >>= \ datum ->
42 newMVar () >>= \ ack ->
43 return (CVar datum ack)
45 putCVar (CVar datum ack) val
50 getCVar (CVar datum ack)
51 = takeMVar datum >>= \ val ->