how to implement timeouts for IO operations?

what is the Right Way to impose a timeout condition on IO actions? the GHC manual says ( http://www.haskell.org/ghc/docs/5.00/set/select.html ) "use threadDelay and asynchronous exceptions". When I do this: timed :: Int -> IO a -> IO a timed d action = do let timer = do threadDelay d throw $ AssertionFailed "timer expired" t <- forkIO timer x <- action killThread t return x it seems to work, but how do I turn off the "Fail: thread killed" messages? -- -- Johannes Waldmann ---- http://www.informatik.uni-leipzig.de/~joe/ -- -- joe@informatik.uni-leipzig.de -- phone/fax (+49) 341 9732 204/252 --

Wed, 18 Apr 2001 14:52:12 +0200 (MET DST), Johannes Waldmann
it seems to work, but how do I turn off the "Fail: thread killed" messages?
import Exception t <- block $ forkIO $ catchJust asyncExceptions (unblock timer) (\_ -> return ()) Perhaps the message about killing a thread will not appear in future versions of ghc. A discussion about it on this list stopped six weeks ago. -- __("< Marcin Kowalczyk * qrczak@knm.org.pl http://qrczak.ids.net.pl/ \__/ ^^ SYGNATURA ZASTÊPCZA QRCZAK

Thank you, Marcin. Your reply seems to imply that the structure of the program I posted was basically OK? A slightly related point, I was having difficulties with TimeDiff values - I got negative tdPicosecs sometimes, and some functions (show) even dumped core. Are such problems known (I am using a binary ghc-4.08)? For the moment, I switched to Posix.EpochTime. I found TimeDiff and ClockTime quite restrictive: they are not instances of Num, I can't add/subtract/negate TimeDiffs; and I cannot generate a zero ClockTime. (In my application, I need to simulate two clocks, where always one is running, the other one is stopped - as used in tournament Chess.) -- -- Johannes Waldmann ---- http://www.informatik.uni-leipzig.de/~joe/ -- -- joe@informatik.uni-leipzig.de -- phone/fax (+49) 341 9732 204/252 --

On Wed, Apr 18, 2001 at 21:36:04 +0200, Johannes Waldmann wrote:
A slightly related point, I was having difficulties with TimeDiff values - I got negative tdPicosecs sometimes, and some functions (show) even dumped core.
Are such problems known (I am using a binary ghc-4.08)? For the moment, I switched to Posix.EpochTime.
I found TimeDiff and ClockTime quite restrictive: they are not instances of Num, I can't add/subtract/negate TimeDiffs; and I cannot generate a zero ClockTime. (In my application, I need to simulate two clocks, where always one is running, the other one is stopped - as used in tournament Chess.)
Despite my humble try, TimeDiff and ClockTime (Time.lhs) are still b0rked. Use on your own risk (and read the comments at the top). Sorry for that... Cheers, Michael

Johannes Waldmann writes: | Thank you, Marcin. | | Your reply seems to imply that the structure | of the program I posted was basically OK? Another way appears as an example in Tackling The Awkward Squad (in the Using Asynchronous Exceptions section). It's a bit more liberal in its use of threads, doing forkIO twice within parIO, but the overall effect is pretty similar to your function's. timeout :: Int -> IO a -> IO (Maybe a) timeout n a = parIO (do { r <- a; return (Just r) }) (do { threadDelay n; return Nothing })

timeout n a = parIO (do { r <- a; return (Just r) }) (do { threadDelay n; return Nothing })
oh, I this looks better (to me) since it doesn't use exceptions! does parIO kill the `other' thread? still I don't find parIO (in ghc-4.08), in what module is it? -- -- Johannes Waldmann ---- http://www.informatik.uni-leipzig.de/~joe/ -- -- joe@informatik.uni-leipzig.de -- phone/fax (+49) 341 9732 204/252 --

OK, I was being a bit stupid this morning. Now I see that `parIO' is not in the ghc libs, but programmed explicitely in the `Tackling the awkward sqaud' paper. http://research.microsoft.com/~simonpj/papers/marktoberdorf.htm -- -- Johannes Waldmann ---- http://www.informatik.uni-leipzig.de/~joe/ -- -- joe@informatik.uni-leipzig.de -- phone/fax (+49) 341 9732 204/252 --
participants (4)
-
Johannes Waldmann
-
Michael Weber
-
qrczak@knm.org.pl
-
Tom Pledger