The Reaper
interface provides facilities for controlling and
harvesting threads.
INTERFACEAReaper ; IMPORT Thread; TYPE T <: Public; Public = Private OBJECT METHODS init(): T; END; Private <: ROOT;
Reaper.T
manages a set of threads. It allows you to fork several
threads, and then wait until any one of them has terminated. You do not
need to know a priori which thread will terminate first.
After one of the threads has terminated (or, actually, at any time), you can alert all of the remaining threads, for example, to terminate all of them prematurely.
We say that the threads managed by the reaper are registered with it.
A thread is registered with the reaper at the time it is forked. To
achieve that, use the following procedure to fork the thread, instead
of Thread.Fork
.
PROCEDURE Fork(reaper: T; cl: Thread.Closure): Thread.T;
Fork a thread, and register it with the reaper.
When a registered thread is about to terminate, it should notify its reaper via the following procedure.
PROCEDURE Dying(reaper: T);
Called by a thread to inform the reaper that it is about to exit.
The following procedures block until the next registered thread has terminated. Then they perform the equivalent of
Thread.Join
on the
terminated thread, and pass back the results.
PROCEDURE JoinNext(reaper: T; VAR thr: Thread.T; VAR ret: REFANY): BOOLEAN;
If there are still registered threads that have not died, wait for the next one to die. When that occurs, returnTRUE
, passing back the thread's handle and return value. If there are no more registered threads alive, returnFALSE
.
PROCEDURE AlertJoinNext(reaper: T; VAR thr: Thread.T; VAR ret: REFANY): BOOLEAN RAISES {Thread.Alerted};
An alertable version of JoinNext
.
To alert all registered threads that have not yet terminated, use the procedure below.
PROCEDURE AlertAll(reaper: T);
Alerts all the registered threads that are still alive.
END Reaper.