logo       

Implementing computations with timeout: msg#00088

lang.haskell.cafe

Subject: Implementing computations with timeout

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


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise