Last active
February 14, 2019 06:50
-
-
Save cblp/a5a91760ea47f5445a3439b81540b335 to your computer and use it in GitHub Desktop.
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
| {-# LANGUAGE OverloadedStrings #-} | |
| import Control.Concurrent (forkIO) | |
| import Control.Monad (void) | |
| import Data.IORef (IORef, atomicModifyIORef, newIORef, | |
| readIORef) | |
| import Data.Map.Strict (Map) | |
| import qualified Data.Map.Strict as Map | |
| import Data.Text (Text) | |
| type Storage = IORef (Map Text Integer) | |
| main :: IO () | |
| main = do | |
| storage <- newIORef Map.empty | |
| void $ forkIO $ | |
| writer storage | |
| void $ forkIO $ do | |
| answer <- reader storage | |
| print answer | |
| writer :: Storage -> IO () | |
| writer r = atomicModifyIORef r $ \m -> (Map.insert "answer" 42 m, ()) | |
| reader :: Storage -> IO (Maybe Integer) | |
| reader r = Map.lookup "answer" <$> readIORef r |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment