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 writeCVar, --:: CVar a -> a -> IO ()
16 readCVar, --:: CVar a -> IO a
25 @MVars@ provide the basic mechanisms for synchronising access to a shared
26 resource. @CVars@, or channel variables, provide an abstraction that guarantee
27 that the producer is not allowed to run riot, but enforces the interleaved
28 access to the channel variable,i.e., a producer is forced to wait up for
29 a consumer to remove the previous value before it can deposit a new one in the @CVar@.
34 = CVar (MVar a) -- prod -> cons
35 (MVar ()) -- cons -> prod
37 newCVar :: IO (CVar a)
38 writeCVar :: CVar a -> a -> IO ()
39 readCVar :: CVar a -> IO a
42 = newEmptyMVar >>= \ datum ->
43 newMVar () >>= \ ack ->
44 return (CVar datum ack)
46 writeCVar (CVar datum ack) val
51 readCVar (CVar datum ack)
52 = takeMVar datum >>= \ val ->