summaryrefslogtreecommitdiff
path: root/testsuite/tests/concurrent/should_run/conc010.hs
blob: 52acb97a359e670453aaba4a9a40da0cf732a30c (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
26
27
28

module Main where

import Control.Concurrent
import Control.Exception

-- Raise an exception in another thread.  We need a lot of synchronisation here:

--   - an MVar for the second thread to block on which it waits for the
--     signal (block)

--   - an MVar to signal the main thread that the second thread is ready to
--     accept the signal (ready)

--   - an MVar to signal the main thread that the second thread has received
--     the signal (ready2).  If we don't have this MVar, then the main
--     thread could exit before the second thread has time to print
--     the result.

main = do 
  block  <- newEmptyMVar
  ready  <- newEmptyMVar
  ready2 <- newEmptyMVar
  id <- forkIO (Control.Exception.catch (putMVar ready () >> takeMVar block) 
		(\e -> putStr (show (e::SomeException)) >> putMVar ready2 ()))
  takeMVar ready
  throwTo id (ErrorCall "hello")
  takeMVar ready2