{-# OPTIONS_GHC -fglasgow-exts #-} module Main where import ErlActors import Control.Concurrent import Control.Concurrent.Chan import Control.Concurrent.MVar producer :: Int -> (Int -> IO t2) -> (Int -> IO t2)-> Process Int -> IO () producer 0 send1 send2 self = do return () producer acc send1 send2 self = do send1 acc send2 acc producer (acc - 1) send1 send2 self consumer :: Int -> Int -> (Process Int) -> IO () consumer cid 0 self = do return () consumer cid acc self = let --m_print:: Maybe (Message Int) -> IO () m_print (Just (Message _ message )) = print $ "consumer" ++ (show cid) ++ ": " ++ (show acc) ++ " message: " ++ show message m_print Nothing = print $ "consumer:" ++ (show cid) ++ ": " ++ (show acc) ++ show( Nothing::Maybe Int ) in do must_receive self (\_-> True) m_print consumer cid (acc - 1) self feedback :: Chan a -> a -> IO () feedback = writeChan --wait for all children to terminate, they should write to their fini MVar wait_for_children [c] = do takeMVar c >> return () wait_for_children (c:cs) = do takeMVar c >> wait_for_children cs main = do fbChan <- newChan fb1 <- newChan forkIO $ runProcess (feedback fbChan) (consumer 1 10000) ( endp, reply ) <- (readChan fbChan)::IO (MVar (), Int -> IO()) forkIO $ runProcess (feedback fbChan) (consumer 2 1000) ( endp1, reply1 ) <- (readChan fbChan)::IO (MVar (), Int -> IO()) forkIO $ runProcess (feedback fbChan) (producer 2000 reply reply1) ( endp2, reply2 ) <- (readChan fbChan)::IO (MVar (), Int -> IO()) wait_for_children [endp, endp1] return ()