Last active
August 29, 2015 14:01
-
-
Save waldheinz/dfb56be1d9b19f24eafc to your computer and use it in GitHub Desktop.
ReadWriteLock fail
This file contains 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
module Main where | |
import Control.Concurrent ( forkIO, threadDelay ) | |
import Control.Monad ( forever, replicateM_ ) | |
import qualified Control.Concurrent.ReadWriteLock as RWL | |
import System.Random ( randomRIO ) | |
main :: IO () | |
main = do | |
lock <- RWL.new | |
let | |
reader = forever $ do | |
randomRIO (0, 100) >>= threadDelay | |
RWL.withRead lock $ putStr "r" | |
writer = forever $ do | |
randomRIO (0, 100) >>= threadDelay | |
RWL.withWrite lock $ putStr "w" | |
replicateM_ 10 $ forkIO reader | |
replicateM_ 10 $ forkIO writer | |
writer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An example run (GHC 7.6.3, Linux 64):