Hi, all, I’m reading Concurrent Haskell paper and I don’t get what is CVar. It looks like MVar but with additional acknowledgment. I don’t see difference between MVar and CVar.
I’ve also found this repo where guy plays around with the primitives, but when I rewrote CVar example, I, again, didn’t see difference
Could someone help me with that?
The code just for reference
newtype CVar a
= CVar
( MVar a, -- Producer writes, Consumer reads
MVar () -- Consumer writes, Producer reads
)
newCVar :: IO (CVar a)
newCVar = do
w <- newEmptyMVar
r <- newEmptyMVar
putMVar r ()
return $ CVar (w, r)
putCVar :: CVar a -> a -> IO ()
putCVar (CVar (w, r)) v =
print "consumed ackVar"
>> takeMVar r
>> print "publishing dataVar"
>> putMVar w v
getCVar :: CVar a -> IO a
getCVar (CVar (w, r)) =
print "consumed dataVar"
>> takeMVar w
>>= \v ->
print "publishing ackVar"
>> putMVar r ()
>> return v