
Taral wrote:
On 11/7/06, Simon Marlow
wrote: So 'timeout N E' should behave exactly the same as E as long as E doesn't time out. Three difficulties with this:
1. if E raises an exception, you want the exception propagated to the parent.
2. if another thread throws an exception to this thread, E should receive the exception.
3. E should get the same result from myThreadId.
I think 2 and 3 go together, and require an exception. Here's my try:
data Timeout = Timeout deriving Typeable
timeout n f | n < 0 = fmap Just f | n == 0 = return Nothing | otherwise = do pt <- myThreadId bracket (forkIO (threadDelay n >> throwDynTo pt Timeout)) (killThread) (fmap Just f) `catchDyn` \Timeout -> return Nothing
Nearly - but this one doesn't nest. Also, it's possible for the Timeout exception to leak. Cheers, Simon