[Haskell-cafe] Concurrency question
akamaus at gmail.com
Tue Sep 6 02:34:24 EDT 2005
> I believe you're just observing lazy evaluation at work. The IO
> computation that you're forking is (return $ resolve cnf). `resolve` is
> a pure function. Hence the forked computation succeeds immediately--and
> the thread terminates (successfully)--without evaluating (resolve cnf).
> It isn't until the case arm that begins "Just (ans, stats) ->" that the
> result of (resolve cnf) is demanded and hence evaluation of (resolve
> cnf) begins. But this is too late for the timeout to have the intended
> How to fix? You need to demand (enough of) the result of (resolve cnf)
> before returning from the IO computation. What "enough of" means
> depends on how `resolve` is written. You may find the DeepSeq module I
> wrote (see
> http://email@example.com/msg15819.html) helpful.
I've just tried DeepSeq as you proposed.
>timeout :: DeepSeq a => Int -> IO a -> IO (Maybe a)
>timeout n t = do res <- par_io timer thr --timer
> return res
> where thr = do res <- t
> return $!! Just res
> timer = do threadDelay n
> return Nothing
All works perfectly now! From now I'll pay more attention to evaluation order.
Thank you for your help and attention.
More information about the Haskell-Cafe