Skip to content

Instantly share code, notes, and snippets.

@jprupp
Created March 8, 2018 16:35
Show Gist options
  • Save jprupp/dd5fc1aec3676b5ec9af14cb6ebbdacd to your computer and use it in GitHub Desktop.
Save jprupp/dd5fc1aec3676b5ec9af14cb6ebbdacd to your computer and use it in GitHub Desktop.
Toy actor that counts
#!/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