Created
June 11, 2016 20:28
-
-
Save cartazio/1fec6bed2cf7e82fb17ae2f79f37ee87 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. | |
| 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 readT } ) ; if x then putStrLn "wippeee" else putStrLn "wattt") | |
| readTBQueue readTChan readTMVar readTQueue readTVar readTVarIO | |
| 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 $ writeT | |
| writeTBQueue writeTChan writeTQueue writeTVar | |
| Prelude Control.Concurrent.STM CC> atomically $ writeTVar (vars ! 6) False | |
| <interactive>:7:30: error: | |
| Variable not in scope: (!) :: [TVar Bool] -> Integer -> TVar Bool | |
| Prelude Control.Concurrent.STM CC> atomically $ writeTVar (vars ! 6 :) False | |
| Prelude Control.Concurrent.STM CC> :t (!) | |
| <interactive>:1:1: error: Variable not in scope: ! | |
| Prelude Control.Concurrent.STM CC> import Data.List (!) | |
| <interactive>:9:19: error: parse error on input ‘!’ | |
| Prelude Control.Concurrent.STM CC> import Data.List ((!)) | |
| <interactive>:1:19: error: Module ‘Data.List’ does not export ‘(!)’ | |
| Prelude Control.Concurrent.STM CC> :t (!!) | |
| (!!) :: [a] -> Int -> a | |
| 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
theres some fun racing btween the other thread and GHCI for writing to the terminal :)