-
-
Save joneshf/ba7a4e6744186f5148485d8d7d0b5918 to your computer and use it in GitHub Desktop.
a little demo GHCI transcript illustrating STM and concurrency
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
GHCi, version 8.0.1: http://www.haskell.org/ghc/ :? for help | |
Prelude> import Control.Concurrent.STM | |
Prelude Control.Concurrent.STM> vars <- atomically $ mapM (\ _ -> newTVar False) [1..10] | |
Prelude Control.Concurrent.STM> length vars | |
10 | |
Prelude Control.Concurrent.STM> :t vars | |
vars :: [TVar Bool] | |
Prelude Control.Concurrent.STM> import Control.Concurrent as CC | |
Prelude Control.Concurrent.STM CC> CC.forkIO ( do x <- atomically (do { xs <- mapM readTVar vars ; if and xs then return True else retry } ) ; if x then putStrLn "wippeee" else putStrLn "wattt") | |
ThreadId 489 | |
Prelude Control.Concurrent.STM CC> atomically $ writeTVar (vars !! 6) False | |
Prelude Control.Concurrent.STM CC> atomically $ writeTVar (vars !! 3) True | |
Prelude Control.Concurrent.STM CC> atomically $ writeTVar (vars !! 3) True | |
Prelude Control.Concurrent.STM CC> atomically $ writeTVar (vars !! 3) True | |
Prelude Control.Concurrent.STM CC> mapM (\x -> atomically $ writeTVar x True) vars | |
[(),(),(),()w,i(p)p,e(e)e, | |
(),(),(),()] | |
Prelude Control.Concurrent.STM CC> vars <- atomically $ mapM (\ _ -> newTVar False) [1..10] | |
Prelude Control.Concurrent.STM CC> CC.forkIO ( do x <- atomically (do { xs <- mapM readTVar vars ; if or xs then return True else retry } ) ; if x then putStrLn "wippeee" else putStrLn "wattt") | |
ThreadId 826 | |
Prelude Control.Concurrent.STM CC> mapM (\x -> atomically $ writeTVar x True) vars | |
Prelude Control.Concurrent.STM CC> x <- atomically $ writeTVar (vars !! 3) True | |
wippeePrelude Control.Concurrent.STM CC> e |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment