This example is included in the package.
---------------------------------------------------------------------------------- | Producer Consumer Example for channels using 'Futures.withFuturesDo'.-- | Main entry point of the example. channelExample implements a consumer producer-- | scenario without futures. channelExampleF uses futures for evaluation.channelExampleF :: IO ()
channelExampleF = Futures.withFuturesDo channelExample
-- | Producer Consumer Example for channels.-- | Main function.channelExample :: IO ()
channelExample = do putStrLn $ "Producer-Consumer example with channels" channel <- Chan.newChan
Control.Concurrent.forkIO $ (produce 10 channel) Control.Concurrent.forkIO $ (consume channel) -- now wait 10 seconds (because humans eyes are slow) Control.Concurrent.threadDelay $ 10 * 1000000
consume :: (Show a) => Chan.Chan a -> IO b
consume chan = do putStrLn $ "Trying to read..." val <- Chan.readChan chan
putStrLn $ "read new value: " ++ show val
--Control.Concurrent.threadDelay oneSecond consume chan produce :: (Num a) => a -> Chan.Chan a -> IO ()
produce n chan = do case n of 0 -> Chan.writeChan chan n
otherwise -> do Chan.writeChan chan n
--wait one second because the concurrent thread should have a try Control.Concurrent.threadDelay 1000000
produce (n-1) chan