I'm interested in running a pause-able computation as a separate thread that may itself spawn its own threads. I'd like to be able to pause the parent thread, and have it transitively pause the child threads it has spawned (as well as any of their descendants). What approaches do people use to achieve this?
I'm aware that I can set up transitive resumption by calling (thread-resume child parent)
after spawning a child. This is close to the kind of relationship I'd like to set up, but for suspension as well.
I am also aware that I could spawn all the child threads using thread/suspend-to-kill
with a particular custodian, and then shutdown that custodian to suspend, and (thread-resume parent new-custodian)
to resume everything. However, using thread/suspend-to-kill
will prevent me from being able to permanently stop a thread, interfering with some uses of thread-wait
.
Is there already a convenient way to do this that I'm overlooking?
Otherwise, what I had in mind was to set up a power grid-like directed graph. Threads are running if and only if they are receiving power.
Power would transmit from an input node to an output node whenever the input node is both receiving power itself and is not currently switched off.
The nodes would be threads, junctions (that can be switched off) used for composing groups, and root power sources (that can also be switched off). Newly-created threads and junctions can be attached to one or more input nodes. To suspend a network of threads, we switch off all of its root power sources. For the pause-able computation situation I described, I'd have one root power source I could turn off at any time.
One nice thing about this approach is that it can remember any internal child thread suspensions that were made, so that when power is turned off and then restored again, those suspended threads remain suspended. That is, to perform an internal suspension, we switch off one or more internal junctions, whose off state is remembered regardless of how power flows. In contrast, normal transitive thread-resume
will undo those internal suspensions, causing everything to resume, which may not be desired.
Anyway, I'm interested in hearing about related thread organizational patterns and ideas. Is my desire to have pause-able computations dubious? Should I be thinking about this problem in a different way? Please let me know.