how to implement timeouts for IO operations?

Tom Pledger Tom.Pledger@peace.com
Thu, 19 Apr 2001 09:15:16 +1200


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 })