C Collections

cthreadpool · Section 3

cthrpool_construct

cthrpool_construct - declare and initialise a thread pool in one step

C Collections (libccollections, -lccollections)

#include <ccollections/cthreadpool.h>
 
cthrpool_construct(name, num_threads, queue_capacity);

Declares a cthrpool variable called name and initialises it with
cthrpool_create(), and calls ccol__fatal_err() if the creation fails.
 
queue_capacity selects the queue mode:
  0                       ->  unbounded queue. submit never blocks on
                               capacity, and the only failure is a
                               lack of memory.
  N > 0                   ->  bounded queue of capacity N. submit
                               blocks when the queue is full, and
                               try_submit returns ccol_container_full
                               instead of blocking.

name            Variable name for the pool handle.
num_threads     Number of worker threads to create (must be >= 1).
queue_capacity  Maximum pending tasks (0 for unbounded). The macro
                calls ccol__fatal_err() for ccol_invalid_size, as it
                does for every other failure of the creation.

The macro calls ccol__fatal_err() on failure, so call cthrpool_create()
directly if you must handle an error gracefully.

cthrpool_construct(pool, 4, 256);
cthrpool_submit(pool, my_fn, my_arg, NULL);
cthrpool_wait(pool);
cthrpool_shutdown_drain(pool);
cthrpool_destroy(pool);
 
/* Unbounded queue */
cthrpool_construct(pool2, 8, 0);
cthrpool_submit(pool2, heavy_fn, NULL, NULL);
cthrpool_shutdown_drain(pool2);
cthrpool_destroy(pool2);

cthrpool_construct_scoped(3), cthrpool_declare(3), cthrpool_create(3), cthrpool_submit(3), cthrpool_shutdown_drain(3), cthrpool_destroy(3)