boost::asio::thread_pool

A simple fixed‐size thread pool.

Synopsis

Declared in <boost/asio/thread_pool.hpp>

class thread_pool
    : public execution_context

Base Classes

Name

Description

execution_context

A context for function object execution.

Types

Name

Description

allocator

basic_executor_type

Executor implementation type used to submit functions to a thread pool.

id

Class used to uniquely identify a service.

service

Base class for all execution context services.

service_maker

Base class for all execution context service makers.

executor_type

Executor used to submit functions to a thread pool.

Enums

Name

Description

fork_event

Fork‐related event notifications.

Member Functions

Name

Description

thread_pool [constructor]

Constructors

~thread_pool [destructor]

Destructor.

attach

Attaches the current thread to the pool.

executor

Obtains the executor associated with the pool.

get_executor

Obtains the executor associated with the pool.

join

Joins the threads.

notify_fork

Notify the execution_context of a fork‐related event.

stop

Stops the threads.

wait

Waits for threads to complete.

Protected Member Functions

Name

Description

destroy

Destroys all services in the context.

shutdown

Shuts down all services in the context.

Friends

Name

Description

boost::asio::thread_pool::basic_executor_type

Executor implementation type used to submit functions to a thread pool.

Non-Member Functions

Name

Description

add_service

(Deprecated: Use make_service().) Add a service object to the execution_context.

has_service

Determine if an execution_context contains a specified service type.

make_service

Creates a service object and adds it to the execution_context.

use_service

Obtain the service object corresponding to the given type.

Description

The thread pool class is an execution context where functions are permitted to run on one of a fixed number of threads.

Thread Safety

Distinct objects: Safe. Shared objects: Safe, with the specific exceptions of the join() wait() and notify_fork() functions. The join() and wait() functions must not be called at the same time as other calls to join() or wait() on the same pool. The notify_fork() function should not be called while any thread_pool function, or any function on an I/O object that is associated with the thread_pool, is being called in another thread. (In effect, this means that notify_fork() is safe only on a thread pool that has no internal or attached threads at the time.)

Submitting tasks to the pool

To submit functions to the thread pool, use the boost::asio::dispatch, boost::asio::post or boost::asio::defer free functions.

For example:

void my_task()
{
  ...
}

...

// Launch the pool with four threads.
boost::asio::thread_pool pool(4);

// Submit a function to the pool.
boost::asio::post(pool, my_task);

// Submit a lambda object to the pool.
boost::asio::post(pool,
    []()
    {
      ...
    });

// Wait for all tasks in the pool to complete.
pool.join();

Created with MrDocs