
Excerpts from Heka Treep's message of Mon Jan 23 15:11:51 -0500 2012:
-------------------------------------------------------------------------------- import Control.Monad.STM import Control.Concurrent import Control.Concurrent.STM.TChan
spawn f = do mbox <- newTChanIO forkIO $ f mbox return mbox
(!) = writeTChan
actor mbox = do empty <- atomically $ isEmptyTChan mbox if empty then actor mbox else do val <- atomically $ readTChan mbox
Uh, don't you want to combine isEmptyChan and readTChan into one single atomic action?
putStrLn val actor mbox
test = do mbox <- spawn actor atomically $ mbox ! "1" atomically $ mbox ! "2" atomically $ mbox ! "3"
-- > test -- 1 -- 2 -- 3 --------------------------------------------------------------------------------
But there are several problems:
* The @actor@ function is busy checking the channel all the time.
GHC's runtime system is clever. It will block appropriately.
* Caller and callee need to perform synchronizations (for the @Chan@) or atomically transactions (for the @TChan@).
The synchronization for Chan is very cheap, and you would have needed to synchronize anyway in Erlang (Erlang message queues are not lock free!) Cheers, Edward