[Haskell-cafe] Implementing computations with timeout

Einar Karttunen ekarttun at cs.helsinki.fi
Fri Jan 7 09:55:05 EST 2005

Tomasz Zielonka <tomasz.zielonka at gmail.com> writes:
>   import Control.Concurrent (forkIO, threadDelay)
>   import Control.Concurrent.STM
>   withTimeout :: Int -> STM a -> IO (Maybe a)
>   withTimeout time fun = do
>       mv <- atomically newEmptyTMVar
>       tid <- forkIO $ do
>           threadDelay time
>           atomically (putTMVar mv ())
>       x <- atomically (fmap Just fun `orElse` (takeTMVar mv >> return Nothing))
>       killThread tid
>       return x

Isn't this buggy if fun just keeps working without throwing an exception
or using retry? I meant wholly inside STM - if we use IO as the
signature then using the TMVar has few advantages over using an MVar.

- Einar Karttunen

More information about the Haskell-Cafe mailing list