boost::capy::thread_pool

Distributes posted work across a fixed group of worker threads via a shared queue.

Synopsis

class thread_pool
    : public execution_context

Description

Use this when you need to run coroutines on multiple threads without the overhead of creating and destroying threads for each task. Work items are distributed across the pool using a shared queue.

Thread Safety

Distinct objects: Safe. Shared objects: Safe for get_executor, join, and stop. Unsafe for construction and destruction.

Example

capy::task<void> some_task() { co_return; }

void run_on_pool()
{
    capy::thread_pool pool(4);  // 4 worker threads
    auto ex = pool.get_executor();
    capy::run_async(ex)(some_task());  // start work; tracked so join() waits for it
    pool.join();  // wait for outstanding work to complete
    // pool destructor stops the pool and abandons any work still queued
}

join() waits only for work that holds outstanding‐work counting, which run_async (and make_work_guard) provide. A bare executor_type::post() does not register outstanding work, so join() does not wait for it.

Base Classes

Name

Description

execution_context

Registers, looks up, and shuts down service objects owned by a derived context.

Types

Name

Description

executor_type

Runs continuations on the worker threads of a thread_pool.

service

Gives a derived service a shutdown() hook, run when its owning execution_context is destroyed.

Member Functions

Name

Description

thread_pool [constructor]

Constructors

~thread_pool [destructor]

Destroy the thread pool.

operator=

Assignment operators

find_service

Return a pointer to the service of type T, or nullptr.

get_executor

Return an executor for this thread pool.

get_frame_allocator

Return the memory resource used for coroutine frame allocation.

has_service

Return true if a service of type T exists.

join

Wait for all outstanding work to complete.

make_service

Construct and add a service.

set_frame_allocator

set_frame_allocator overloads

stop

Request all worker threads to stop.

target

Return a pointer to this context if it matches the requested type.

use_service

Return a reference to the service of type T, creating it if needed.

Protected Member Functions

Name

Description

destroy

Destroy all services.

shutdown

Shut down all services.

Non-Member Functions

Name

Description

get_system_context

Return the process‐wide system thread pool.

Created with MrDocs