summaryrefslogtreecommitdiff
path: root/testsuite/tests/concurrent/should_run/throwto002.hs
blob: c9857f1f1ecc0716b45150671bf6cd7bee83e682 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{-# LANGUAGE DoRec, ScopedTypeVariables #-}
import Control.Concurrent
import Control.Exception
import Data.Array
import System.Random
import System.Environment
import Control.Monad
import GHC.Conc
import Data.IORef
import Prelude hiding (catch)

main = do
  r <- newIORef 0
  rec
    t1 <- block $ forkIO (thread r t2)
    t2 <- block $ forkIO (thread r t1)
  threadDelay 1000000
  readIORef r >>= print

thread r t = run
  where 
    run = (unblock $ forever $ do killThread t
                                  i <- atomicModifyIORef r (\i -> (i + 1, i))
                                  evaluate i)
             `catch` \(e::SomeException) -> run