Skip to content

Instantly share code, notes, and snippets.

@creichert
Forked from cartazio/STM-FUNN.hs
Created June 11, 2016 20:42
Show Gist options
  • Save creichert/039ddcce6901a59f31cf8c9381801f0b to your computer and use it in GitHub Desktop.
Save creichert/039ddcce6901a59f31cf8c9381801f0b to your computer and use it in GitHub Desktop.
a little demo GHCI transcript illustrating STM and concurrency
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment