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
effect.
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://www.mail-archive.com/[email protected]/msg15819.html) helpful.
Dean
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.
_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe