logo       

Re: Implementing computations with timeout: msg#00094

lang.haskell.cafe

Subject: Re: Implementing computations with timeout

Tomasz Zielonka <tomasz.zielonka@xxxxxxxxx> 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


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

News | FAQ | advertise