\begin{slide}{Beispiel (ohne STM)}

\begin{verbatim}
main = do
  let steps = 10000
      threads = GHC.Conc.numCapabilities
  counter <- newIORef 0
  ch <- newChan -- als Latch benutzt
  forM [ 1 ..  threads ] $ \ t -> forkIO $ do
      forM [ 1 .. steps ] $ \ s -> do
          c <- readIORef counter
          writeIORef counter (c + 1)
      writeChan ch ()
  forM [ 1 .. threads ] $ \ s -> readChan ch
  c <- readIORef counter 
  putStrLn $ show c
\end{verbatim}
  
\end{slide}

\begin{slide}{Beispiel (mit STM)}

\begin{verbatim}
main = do
  let steps = 10000
      threads = GHC.Conc.numCapabilities
  counter <- atomically $ newTVar 0
  ch <- newChan
  forM [ 1 ..  threads ] $ \ t -> forkIO $ do
    forM [ 1 .. steps ] $ \ s -> atomically $ do
        c <- readTVar counter
        writeTVar counter (c + 1)
    writeChan ch ()
  forM [ 1 .. threads ] $ \ s -> readChan ch
  c <- atomically $ readTVar counter
  putStrLn $ show c
\end{verbatim}
  

\end{slide}
