1 -- !!! test tryTakeMVar
13 r <- timeout 5 (tryTakeMVar m) (putStrLn "timed out!" >> return Nothing)
14 print (r :: Maybe Int)
19 -> IO a -- action to run
20 -> IO a -- action to run on timeout
23 timeout secs action on_timeout
25 threadid <- myThreadId
26 timeout <- forkIOIgnoreExceptions (
27 do threadDelay (secs * 1000000)
28 raiseInThread threadid (ErrorCall "__timeout")
35 ( \exception -> case exception of
36 ErrorCall "__timeout" -> on_timeout
41 forkIOIgnoreExceptions :: IO () -> IO ThreadId
42 forkIOIgnoreExceptions action = IO $ \ s ->
43 case (fork# action_plus s) of (# s1, id #) -> (# s1, ThreadId id #)
45 action_plus = catchAllIO action (\_ -> return ())