[Haskell-cafe] STM, orElse and timed read from a channel
Joel Reymont
joelr1 at gmail.com
Mon Dec 12 05:08:56 EST 2005
While we are waiting for ghc 6.6, could the same effect be achieved
by launching the computation that writes to t with forkIO?
On Nov 29, 2005, at 12:00 PM, Simon Marlow wrote:
> threadDelay is IO-only; there's no way to use threadDelay in an STM
> transaction. For example, if you want to wait for a TVar to go from
> Nothing to Just x with a timeout, you could do this:
>
> readOrTimeout :: TVar (Maybe a) -> Int -> STM (Maybe a)
> readOrTimeout t secs = do
> timeout <- registerTimeout secs
> let check_timeout = do
> b <- readTVar timeout
> if b then return Nothing else retry
> check_t = do
> m <- readTVar t
> case m of
> Nothing -> retry
> Just x -> return x
> atomically $ check_timeout `orElse` check_t
--
http://wagerlabs.com/
More information about the Haskell-Cafe
mailing list