C Collections

cthreadpool · Section 3

cthrpool_declare_scoped

cthrpool_declare_scoped - declare a thread pool variable with automatic RAII destruction

C Collections (libccollections, -lccollections)

#include <ccollections/cthreadpool.h>
 
cthrpool_declare_scoped(name);

The same as cthrpool_declare(), plus a cleanup attribute, so the pool is
destroyed when the variable leaves scope, exactly as cthrpool_destroy()
would destroy it.
 
You must still assign the variable before you use it. If it is still
CTHRPOOL_NULL at the end of scope, the cleanup does nothing.

name  Variable name to declare.

It needs GCC or Clang, because it uses __attribute__((cleanup)).
When the drain behavior matters, run a shutdown yourself before the
end of scope: The shutdown that the destroy runs on its own drains the
queue, and that drain can take any amount of time when the tasks are
long.

{
    cthrpool_declare_scoped(pool);
    pool = cthrpool_create(4, 0, NULL);
    cthrpool_submit(pool, my_fn, my_arg, NULL);
    cthrpool_shutdown_drain(pool);
}  /* pool is destroyed automatically here */

cthrpool_declare(3), cthrpool_construct_scoped(3), cthrpool_destroy(3)