[Haskell-cafe] STM, orElse and timed read from a channel
Simon Marlow
simonmar at microsoft.com
Mon Dec 12 10:29:27 EST 2005
On 12 December 2005 10:09, Joel Reymont wrote:
> While we are waiting for ghc 6.6, could the same effect be achieved
> by launching the computation that writes to t with forkIO?
Certainly. registerTimeout is just an optimisation that avoids creating
a new thread each time.
Cheers,
Simon
> 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
More information about the Haskell-Cafe
mailing list