darcs patch: forkChild, waitForChild, parIO, timeout
simonmarhaskell at gmail.com
Wed Nov 8 05:57:45 EST 2006
> On 11/7/06, Simon Marlow <simonmarhaskell at gmail.com> 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
>> 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))
> (fmap Just f) `catchDyn` \Timeout -> return Nothing
Nearly - but this one doesn't nest. Also, it's possible for the Timeout
exception to leak.
More information about the Libraries