C Collections

cthreadpool · Section 3

cthrpool_shutdown_drain

cthrpool_shutdown_drain - graceful shutdown

C Collections (libccollections, -lccollections)

#include <ccollections/cthreadpool.h>
 
void cthrpool_shutdown_drain(cthrpool pool);

Tells the pool to accept no new task, waits until every task in the
queue and every active task has finished, and then joins every worker
thread. The call blocks until every worker has exited.
 
After the call returns the pool accepts no new task, and
cthrpool_destroy can free the pool struct. Calling it before
cthrpool_destroy is optional: If the caller called neither this function
nor cthrpool_shutdown_immediate(), cthrpool_destroy runs the same drain
itself.
 
A second call, whether it comes after the first one or at the same
time on another thread, does no shutdown work of its own, but it still
blocks until the shutdown that is under way ends. So every caller gets
the same guarantee: Once the call returns, every worker thread has
exited.

pool   Thread pool handle (CTHRPOOL_NULL or a stale/already-destroyed
       handle is a no-op)

Every submit function returns ccol_not_permitted after this call;
cthrpool_submit_future returns NULL and stores ccol_not_permitted in its
rv_out.
Every thread blocked in cthrpool_submit on a full bounded queue is woken
up, and each of them gets ccol_not_permitted.
 
A call to cthrpool_shutdown_drain on pool from inside a task running on
one of pool's worker threads, or from the on_complete callback of that
task, does nothing at all. It doesn't stop the pool from accepting new
tasks, it joins no worker and it doesn't block. A worker thread cannot
join itself, so going on could never wait for the calling thread to
exit, only for every other worker.
 
A cthrpool_shutdown_immediate call can come while this call is still
draining. That call then takes over: It discards whatever is left in
the queue at that moment and cancels the futures of those tasks, and
this call returns once every worker has exited.

cthrpool_shutdown_immediate(3), cthrpool_wait(3), cthrpool_destroy(3)