cthreadpool · Section 3
cthrpool_wait
NAME
cthrpool_wait - block until the pool is idle
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/cthreadpool.h> void cthrpool_wait(cthrpool pool);
DESCRIPTION
Blocks until the task queue is empty and every active task has stopped. It doesn't shut the pool down: Every worker thread stays alive, and the caller can submit new tasks after the call returns. Contract: No other thread should submit a task while cthrpool_wait runs, because with an unbounded queue a submission from another thread can keep this call blocked for ever.
PARAMETERS
pool Thread pool handle (CTHRPOOL_NULL or a stale/already-destroyed handle is a no-op)
NOTES
cthrpool_wait waits for the queue and for the tasks that workers run, but not for the on_complete callbacks of discarded tasks. A cthrpool_shutdown_immediate(3) on another thread empties the queue at once and then calls the on_complete of each discarded task, with ran == false, on its own thread, so cthrpool_wait can return while those callbacks are still running. If you need to know that all of them have finished, wait for the cthrpool_shutdown_immediate call itself to return. By default a pool that a child inherited across fork(2) is treated as idle, because fork(2) doesn't copy the pool's worker threads into the child. cthrpool_wait on such a pool in the child returns at once instead of waiting for progress that no worker thread in that process can ever make. A call to cthrpool_wait on pool from inside a task running on one of pool's worker threads, or from the on_complete callback of that task, is also treated as idle by default and returns at once. The pool counts the calling task among its active tasks until that task returns, so without this rule a wait here would deadlock the calling worker against itself.