Commit c4b8e719 authored by f-a's avatar f-a Committed by Ben Gamari

Fixed "Memory Model" example.

removed whitespace and added relevant imports to the
"Memory Model" example (haddock documentation).

Reviewers: hvr, bgamari

Reviewed By: bgamari

Subscribers: RyanGlScott, rwbarton, thomie, carter

Differential Revision: https://phabricator.haskell.org/D4966
parent 28199a21
......@@ -111,19 +111,23 @@ atomicWriteIORef ref a = do
processor architecture. For example, on x86, loads can move ahead
of stores, so in the following example:
> maybePrint :: IORef Bool -> IORef Bool -> IO ()
> maybePrint myRef yourRef = do
> writeIORef myRef True
> yourVal <- readIORef yourRef
> unless yourVal $ putStrLn "critical section"
>
> main :: IO ()
> main = do
> r1 <- newIORef False
> r2 <- newIORef False
> forkIO $ maybePrint r1 r2
> forkIO $ maybePrint r2 r1
> threadDelay 1000000
> import Data.IORef
> import Control.Monad (unless)
> import Control.Concurrent (forkIO, threadDelay)
>
> maybePrint :: IORef Bool -> IORef Bool -> IO ()
> maybePrint myRef yourRef = do
> writeIORef myRef True
> yourVal <- readIORef yourRef
> unless yourVal $ putStrLn "critical section"
>
> main :: IO ()
> main = do
> r1 <- newIORef False
> r2 <- newIORef False
> forkIO $ maybePrint r1 r2
> forkIO $ maybePrint r2 r1
> threadDelay 1000000
it is possible that the string @"critical section"@ is printed
twice, even though there is no interleaving of the operations of the
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment