Re: [Gauche-devel] Terminating a Job in a Thread Pool
R7RS Scheme scripting engine
Status: Beta
Brought to you by:
shirok
From: Lasse K. <las...@gm...> - 2014-06-25 12:14:36
|
Hi Shiro, thanks for the answer. I thought about how to implement the idea of never interrupting a thread but letting threads terminate by themselves based on flags. In my use case, several threads wait for external events and the program requires to move on whenever one of the events occurs. Say, one thread waits for an external process to terminate and another thread waits for user input, currently via read-line. If the external process terminates first, then read-line should also be terminated. But if the user completes input first, then the external process should be terminated. The actual situation is even more complicated, but let's take this as an example for demonstration. The external process could be handled with process-wait and #t to the nohang flag, called periodically, say every 0.1 seconds. If the external process is to be terminated, I can use process-kill. But how should I handle read-line? Start an external process for it as well? But then I would need a channel to communicate the user input back to the parent process. This is becoming messy. So even though external termination of threads may be unclean, it appears easier to me than anything else I can think of right now. Maybe I'm missing something. If sticking to external termination, would it be possible to have a kind of terminate-job! procedure, a kind of light version of terminate-all! ? This way at least the thread pool would not be invalidated each time, as it happens with terminate-all!. On 06/25/14 13:19, Shiro Kawai wrote: > Hi Lasse, > > In general it is not easy to stop what another thread is doing > safely, reliably and portably. The means the underlying thread > systems provide are tricky to use, and are likely to leave > some parts of the system inconsistent. > The "job termination" API is missing since I haven't come up > a good way to support it generally. > > The best way is to construct your jobs in a way that each job > won't take time indefinitely; split heavy jobs into several > chunks and sequence it, for example. If the job needs to wait > on an outside resource, set a timeout to retry, or poll an external > flag to see if it should give up. > > In the development branch, I implemented a simple inter-thread > communication mechanism at the Gauche VM level, and I hope I can > extend it to support safer way to interrupt another thread. > (It allows to interrupt another thread at "safe" points where > VM state is consistent; the drawback is that you can't interrupt > another thread if it is looping in a C subroutine, or waiting > on a system call.) > > Note that thread-terminate! first tries this safer channel, then > gradually casades up to the brutal means such as TerminateThread. > So, it shuold be considered an emergency means and be avoided > whenever possible. > > > > From: Lasse Kliemann <las...@gm...> > Subject: [Gauche-devel] Terminating a Job in a Thread Pool > Date: Tue, 24 Jun 2014 16:23:18 +0200 > >> Hi, how can I terminate a job which has been added to a thread pool >> (created with 'make-thread-pool') with 'add-job!'? I coudn't find it in >> the documentation, maybe I missed something. Currently, I'm helping >> myself using 'terminate-all!', which is good enough for my use case, but >> certainly not intended for this purpose since the next step in my >> program is to create a new thread pool with 'make-thread-pool' just to >> replace the one that has been invalidated by the call to 'terminate-all!". >> >> Thank you! >> Lasse >> > |