import Control.Concurrent
import System.Random

nestedModification outer inner = do
  modifyMVar_ outer $ \x -> do
    putStrLn ((show x) ++ " starts")
    randomDelay
    yield  -- force this thread to temporarily yield the CPU
    randomDelay
    modifyMVar_ inner $ \y -> return (y + 1)
    putStrLn ((show x) ++ " finishes")
    return (x + 1)



randomDelay = do { r <- randomRIO (100000,500000);
                   threadDelay r}
                  

main = do
  a <- newMVar 1
  b <- newMVar 2
  putStrLn "Start" 
  forkIO $ nestedModification a b
  forkIO $ nestedModification a b
  -- damit Programm nicht terminiert, bevor Kindthreads fertig sind
  putStrLn "Bitte Taste druecken!"  
  c <- getChar 
  putStrLn "Ende!"
  

