Loading queue-base-threading.cabal +7 −0 Original line number Diff line number Diff line Loading @@ -26,6 +26,13 @@ executable try1 , random default-language: Haskell2010 executable try2 main-is: try2.hs ghc-options: -threaded -rtsopts -with-rtsopts=-N build-depends: base , random default-language: Haskell2010 source-repository head type: git location: https://github.com/sylecn/queue-base-threading try2.hs 0 → 100644 +44 −0 Original line number Diff line number Diff line import Control.Monad import Control.Concurrent.MVar import Control.Concurrent import Text.Printf import System.Random fuzz :: IO () fuzz = do seconds <- (getStdRandom random :: IO Double) threadDelay $ round (realToFrac (1000000 * seconds)) counterManager :: MVar Integer -> Chan Integer -> Chan [String] -> IO () counterManager counter counterCh printerCh = forever $ do fuzz incrementBy <- readChan counterCh fuzz newCount <- modifyMVar counter (\i -> return (i + 1, i + 1)) fuzz writeChan printerCh [printf "The count is %d" newCount, "---------------"] fuzz printerManager :: Chan [String] -> IO () printerManager ch = forever $ do fuzz rows <- readChan ch fuzz mapM_ putStrLn rows fuzz main :: IO () main = do counter <- newMVar 0 counterCh <- newChan printerCh <- newChan forkIO $ counterManager counter counterCh printerCh forkIO $ printerManager printerCh writeChan printerCh ["Starting up"] replicateM_ 10 (writeChan counterCh 1) -- join counterCh writeChan printerCh ["Finishing up"] -- join printerCh -- done threadDelay $ 20 * 1000000 Loading
queue-base-threading.cabal +7 −0 Original line number Diff line number Diff line Loading @@ -26,6 +26,13 @@ executable try1 , random default-language: Haskell2010 executable try2 main-is: try2.hs ghc-options: -threaded -rtsopts -with-rtsopts=-N build-depends: base , random default-language: Haskell2010 source-repository head type: git location: https://github.com/sylecn/queue-base-threading
try2.hs 0 → 100644 +44 −0 Original line number Diff line number Diff line import Control.Monad import Control.Concurrent.MVar import Control.Concurrent import Text.Printf import System.Random fuzz :: IO () fuzz = do seconds <- (getStdRandom random :: IO Double) threadDelay $ round (realToFrac (1000000 * seconds)) counterManager :: MVar Integer -> Chan Integer -> Chan [String] -> IO () counterManager counter counterCh printerCh = forever $ do fuzz incrementBy <- readChan counterCh fuzz newCount <- modifyMVar counter (\i -> return (i + 1, i + 1)) fuzz writeChan printerCh [printf "The count is %d" newCount, "---------------"] fuzz printerManager :: Chan [String] -> IO () printerManager ch = forever $ do fuzz rows <- readChan ch fuzz mapM_ putStrLn rows fuzz main :: IO () main = do counter <- newMVar 0 counterCh <- newChan printerCh <- newChan forkIO $ counterManager counter counterCh printerCh forkIO $ printerManager printerCh writeChan printerCh ["Starting up"] replicateM_ 10 (writeChan counterCh 1) -- join counterCh writeChan printerCh ["Finishing up"] -- join printerCh -- done threadDelay $ 20 * 1000000