Implementing channels in haskell - Fighting an uncomfortable team

In the article Grabbing an Uncomfortable Team , Simon Peyton Jones provided a "possible implementation Channel. "

type Channel a = (MVar (Stream a) , -- Read end
                  MVar (Stream a) ) -- Write end (the hole)

type Stream a = MVar (Item a)

data Item a = MkItem a (Stream a)

Now it implements a function putChan :: Channel a -> a -> IO ()like this

putChan (read, write) val
  = do { new_hole <- newEmptyVar ;
         old_hole <- takeMVar write ;
         putMVar write new_hole ;
         putMVar old_hole (MkItem val new_hole) }

The above function takes the MVar out of the record, then puts the empty MVar in it.
Then it writes to the old_hole extracted from the record.

The question is why is he writing old_hole? It was removed from the record, and its scope is limited only by the current block, then what's the difference?

+4
source share
1 answer

The question is why is he writing old_hole? It was removed from the record, and its scope is limited only by the current block, then what's the difference?

. old_hole . newChan :

newChan = do { 
    read <- newEmptyMVar ;
    write <- newEmptyMVar ;
    hole <- newEmptyMVar ;
    putMVar read hole ;
    putMVar write hole ;
    return (read,write) }

, newChan "old_hole" putChan MVar hole newChan. , old_hole - MVar read.

, - . , - "" MVar, , "" MVars, mainting .

, , Control.Concurrent.Chan

+4

Source: https://habr.com/ru/post/1570517/


All Articles