31 May
2009
31 May
'09
5:23 a.m.
2009/5/30 Cetin Sert <cetin.sert@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