
7 Jan
2005
7 Jan
'05
8:31 a.m.
Hello What is the best way of doing an computation with a timeout? A naive implementation using two threads is easy to create - but what is the preferred solution? withTimeout :: forall a. Int -> IO a -> IO (Maybe a) withTimeout time fun = do mv <- newEmptyMVar tid <- forkIO (fun >>= tryPutMVar mv . Just >> return ()) forkIO (threadDelay time >> killThread tid >> tryPutMVar mv Nothing >> return ()) takeMVar mv btw How would I do the same with the new STM abstraction? - Einar Karttunen