[Haskell-cafe] Concurrent Haskell Actions with Timeout

Sebastian Sylvan sebastian.sylvan at gmail.com
Sat May 30 17:23:51 EDT 2009


2009/5/30 Cetin Sert <cetin.sert at gmail.com>

> Hi how could one implement a function in concurrent haskell that either
> returns 'a' successfully or due timeout 'b'?
>
> timed :: Int → IO a → b → IO (Either a b)
> timed max act def = do
>


Something like (warning, untested code - no compiler atm).

timed timeout act fallback = do
   res <- newEmptyMVar
   tid <- forkIO $ act >>= writeMVar res
   threadDelay timeout
   stillRunning <- isEmptyMVar res
   if stillRunning then killThread tid >> return fallback else takeMVar res

-- 
Sebastian Sylvan
+44(0)7857-300802
UIN: 44640862
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090530/b5af48b6/attachment.html


More information about the Haskell-Cafe mailing list