What is the recommended way of implementing the function:
timeOut :: Int -> IO a -> IO (Maybe a)
which takes a maximum number of seconds (say), an IO action, and tries to run the IO action in the specified time. If it succeeds, it returns just the answer, otherwise it returns Nothing.
Do I have to start two threads, one that waits and then throws an exception, the other that evaluates the IO action?
Or is there a better way? (I thought of asynchronous exceptions...)
With asyncrhonous exceptions as they are implemented currently, you only need one extra thread. If we ever get around to changing the semantics as per the async exceptions paper, then you'll probably need two threads. Anyway, the way to do it is: - fork off a thread which sleeps for the specified period and then raises an exception in the parent thread - in the parent thread, run the IO action. If it completes, kill the child thread. You are guaranteed by the semantics of killThread that both threads can't kill each other simultaneously. Cheers, Simon
On Tuesday 10 September 2002 03:58 am, Simon Marlow wrote:
With asyncrhonous exceptions as they are implemented currently, you only need one extra thread. If we ever get around to changing the semantics as per the async exceptions paper, then you'll probably need two threads.
How do they differ, precisely? Is it whether throwTo is synchronous or asynchronous? I seem to recall that we changed the semantics from synchronous to asynchronous, late-ish in the game, in order to stream-line the semantics. Is there any reason (other than lack of time) not to bring GHC in line with the paper? Koen: the paper has a two thread timeout. It should work even if throwTo is synchronous ... A -- Andy Moran Ph. (503) 526 3472 Galois Connections Inc. Fax. (503) 350 0833 3875 SW Hall Blvd. http://www.galois.com Beaverton, OR 97005 moran@galois.com
participants (2)
-
Andy Moran -
Simon Marlow