Created
March 8, 2018 16:35
-
-
Save jprupp/dd5fc1aec3676b5ec9af14cb6ebbdacd to your computer and use it in GitHub Desktop.
Toy actor that counts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env stack | |
-- stack --resolver lts-10.8 runghc --package nqe-0.1.0.0 | |
import Control.Monad | |
import Control.Concurrent.NQE | |
data CounterMessage = GetCounter (Reply Integer) | |
counter :: Inbox CounterMessage -> IO () | |
counter inbox = do | |
box <- newTVarIO 0 | |
forever $ do | |
GetCounter reply <- receive inbox | |
atomically $ do | |
n <- readTVar box | |
reply n | |
modifyTVar box (+1) | |
printer :: Inbox CounterMessage -> IO () | |
printer inbox = do | |
replicateM_ 5 $ do | |
i <- GetCounter `query` inbox | |
print i | |
main :: IO () | |
main = do | |
inbox <- Inbox <$> newTQueueIO | |
super <- Inbox <$> newTQueueIO | |
supervisor KillAll super (actors inbox) | |
where | |
actors inbox = [counter inbox, printer inbox] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment