Is this just a problem of spawning too many forkIO resources that never produce a result?<div><br></div><div>I was thinking of trying something like the following in System.Timeout's place:</div><div><br></div><div><div>
> module Main where</div><div><br></div><div>> import Control.Concurrent.MVar</div><div>> import Control.Concurrent</div><div>> import Data.Maybe</div><div><br></div></div><div><br></div><div><div>> timeout :: Int -> IO a -> IO (Maybe a)</div>
<div>> timeout time action = do</div><div>> someMVar <- newEmptyMVar -- MVar is a Maybe</div><div>> timeoutThread <- forkIO $ nothingIzer time someMVar</div><div>> forkIO $ actionRunner action someMVar timeoutThread</div>
<div>> takeMVar someMVar >>= return</div><div>> where </div><div>> nothingIzer time mvar = threadDelay time >> putMVar mvar Nothing</div><div>> actionRunner action mvar timeoutThread = do </div>
<div>> res <- action</div><div>> killThread timeoutThread</div><div>> putMVar mvar $ Just res</div><div><br></div><div>> main :: IO ()</div>
<div>> main = do </div><div>> res <- timeout (5 * 10 ^ 6) (getLine >>= putStrLn)</div><div>> case res of</div><div>> Nothing -> putStrLn "Timeout"</div><div>> Just x -> putStrLn "Success"</div>
<div><br></div><div><br></div><br><div class="gmail_quote">On Tue, Mar 23, 2010 at 11:31 AM, Roel van Dijk <span dir="ltr"><<a href="mailto:vandijk.roel@gmail.com">vandijk.roel@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
I tried a few things. First I added another timeout to main, so the<br>
program kills itself after a few seconds.<br>
<br>
doit :: IO (Maybe ())<br>
doit = timeout 12000000 $ {- yield >> -} return ()<br>
<br>
main :: IO ()<br>
main = do _ <- timeout 5000000 $ forever doit<br>
return ()<br>
<br>
This program failed to terminate. But when I compiled -with threaded<br>
and added a yield to doit, it worked (kinda). If the timeout in doit<br>
is not too long, like 200 milliseconds, the program has constant space<br>
usage. But when I increased the timeout in doit to 12 seconds I got a<br>
stack overflow.<br>
<br>
I'll investigate further when I have more time.<br>
<br>
Regards,<br>
<font color="#888888">Roel<br>
</font></blockquote></div><br></div>