[Haskell-cafe] synchronous channels in STM
claus.reinke at talk21.com
Wed Oct 8 19:39:16 EDT 2008
>I was wondering if it was possible to implement synchronous channels
> within STM. In particular, I'd like to have CSP-like send and recv primitives
> on a channel that each block until the other side arrives to complete
> the transaction.
Assuming that retry blocks until something changes, you could associate
a channel with a thread that encapsulates the transaction. Somewhat like this?
sender write confirm = do
atomically $ putTMVar write "hi"
atomically $ takeTMVar confirm
putStrLn "sender done"
receiver read = do
r <- atomically $ takeTMVar read
putStrLn $ "receiver done: "++r
channel read write confirm = atomically $ do
w <- takeTMVar write
putTMVar read w
putTMVar confirm ()
main = do
(read,write,confirm) <- atomically $ do
read <- newEmptyTMVar
write <- newEmptyTMVar
confirm <- newEmptyTMVar
forkIO (channel read write confirm)
forkIO (threadDelay 3000000 >> receiver read)
threadDelay 5000000 >> sender write confirm
You might also be interested in
More information about the Haskell-Cafe