<div dir="ltr"><div>I suppose my question concerns the more general question of how to create OS-managed threads in GHC.  As I understand it, GHC's concurrency model only exposes its RTS-managed internal threads, which are distributed somehow to OS threads by the scheduler, and this is the source of my timeout problem, because the scheduler never runs.  Contrast this with plain Linux C, where we can do something with pthread_create to call a function in an OS-managed thread directly.  I would have expected there to be a corresponding operation in GHC Haskell ("bound threads" seem not to be it, as they are still scheduled by the RTS) but it does not appear that there is.  Is this because of the need to keep the runtime unified?  Because it seems strange that we are prevented from operating truly independent threads.</div><div><br></div><div>Ryan<br></div></div><br><div class="gmail_quote"><div dir="ltr">On Sat, Nov 17, 2018 at 3:21 PM Ryan Reich <<a href="mailto:ryan.reich@gmail.com">ryan.reich@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div dir="ltr"><div>I want to time out a pure computation.  My experience, and that described in various previous questions here and elsewhere (the best of which is <a href="https://mail.haskell.org/pipermail/haskell-cafe/2011-February/088820.html" target="_blank">https://mail.haskell.org/pipermail/haskell-cafe/2011-February/088820.html</a>), is that this doesn't always work: for instance,</div><div><br></div><div>>>> timeout 1 $ evaluate $ let x = 0 : x in last x</div><div><br></div><div>does not time out because, apparently, the fact that the expression evaluates in constant space (i.e. never allocates) means that it never yields to the timeout monitor thread that would kill it.</div><div><br></div><div dir="ltr">The solution that is described in the other iterations is to embed checkpoints in the expression that do allocate, giving the RTS a chance to switch contexts.  However, in my application, the expression is /arbitrary/ and I do not have the freedom to inject alterations into it.  (Don't argue this point, please.  The expression is arbitrary.)<div><br></div><div>How can I time out a tight loop like the above?  Clearly, it can be done, because I can, say, alt-tab over to another terminal and kill the process, which exploits the operating system's more aggressively pre-emptive scheduling.  Is there a solution using bound threads, say 'forkOS' instead of 'forkIO' in the implementation of 'timeout'?  Unix signals?  Some FFI-based workaround?  Etc.  Keep in mind that notwithstanding that comment, I don't actually want to kill the whole process, but just the one evaluation.<br></div><div><br></div><div>Thanks in advance,</div><div>Ryan Reich<br></div></div></div></div>
</blockquote></div>