System & concurrency

pool.h

A queue-driven thread pool and the FIFO `CPool` convenience alias.

C++23 mc/pool.h
#include <mc/pool.h>

Start the pool before relying on queued work. Stop it from a thread outside the pool; worker callbacks must handle their own exceptions.

CPool is CFIFOPool<std::function<void(void)>>. CFIFOPool inherits the lifecycle methods of CThreadPool.

Jump to a declaration · 11

CThreadPool

template<class Queue> requires requires(Queue q, typename Queue::Item i){ {q.pop(i)} -> std::same_as<bool>; i(); } class CThreadPool

Types, constants & data

using Item = typename Queue::Item;

Methods

start

void start(size_t threadCount);

Starts workers that remove callable items from the queue. Start only once for a FIFO pool; stopping permanently disables its queue.

stop

void stop();

Disables the queue and joins workers. For the FIFO pool, queued work is not drained; wait for required tasks before stopping. The disabled FIFO pool is not restartable.

numThreads

size_t numThreads() const;

Returns the number of workers started and not yet joined by stop(). It does not count pending tasks.

CFIFOPool

template<class I> class CFIFOPool : public CThreadPool<CLQueue<I>>

Types, constants & data

using Item = I;

Methods

push

template<class T> void push(T&& item);

Queues a callable for a worker. Start the pool before expecting execution and catch task failures inside the callable; escaping exceptions are not collected in a future.

operator<<

template<class T> CFIFOPool& operator<<(T&& item);

Queues a callable through push() and returns the pool for chaining.

Free functions & types

Types, constants & data

using CPool = CFIFOPool<std::function<void(void)>>;