Skip to content

Instantly share code, notes, and snippets.

@waldheinz
Last active August 29, 2015 14:01
Show Gist options
  • Save waldheinz/dfb56be1d9b19f24eafc to your computer and use it in GitHub Desktop.
Save waldheinz/dfb56be1d9b19f24eafc to your computer and use it in GitHub Desktop.
ReadWriteLock fail
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
@waldheinz
Copy link
Author

An example run (GHC 7.6.3, Linux 64):

./Main 
rrrrrrrrrrrrrwrrrrrrrrwwwwwwwwwwrrwrrrrrrrrwwwwwwwwwwrrwrrrrrrrrwwwwwwwwwwrrrwrrrrrrrwwwwwwwwwwrrrrwrrrrrrwwrwwwwwwwwrrrrwrrrrrwwrwwwwwwwrwrrrwrrrrwrwrwwwwwwwrrwrrwrrrrwrrwrwwwwwwwrrwrwrrrrwrrwrwwwwwwrrwwrrrwrrwrrrwwwwwwwrrwwrrrwrwrrrrwwwwwwwrrwrwrrrwrrwrrrwwwwwwwrrwrwrrwrrrwrrwwwwwwrwwrrwrrwrrwrrrwwwwwwrwwrrwrrwrrwrrrwwwwwwrwwrrwrrwrrrwrwrwwwwwwrwrrwrrwrrwrrwrwwwwwwwrrrwrrwrrwrrrwwwwwwwwwrrrrwrrrwwrwrrwwwwwwwrrrrwrrrwwwrrrwwwwwwwrrrwrrrrwwwrwrrwwwwwwrrrwrrwwrrwrwrwwrwwrwwwrrrwrwrwwrrwrrwwwwrwwrrrwwrrrwwwrwwrrwwrrwwwwrwrrrwwrrwwrwrrrwwwwwrwrrwrwrrwwwrrrwrwwrwwwwrwrrrwrrwrwwrwrwwwrwwrw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rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrwwrrrrrrrrwrrrwMain: Control.Concurrent.ReadWriteLock.releaseRead: already released
Main: Control.Concurrent.ReadWriteLock.releaseRead: already released
Main: Control.Concurrent.ReadWriteLock.releaseRead: already released
wMain: thread blocked indefinitely in an MVar operation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment