...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
A simple fixed-size thread pool.
class thread_pool : public execution_context
Name |
Description |
---|---|
Executor implementation type used to submit functions to a thread pool. |
|
Executor used to submit functions to a thread pool. |
|
Fork-related event notifications. |
Name |
Description |
---|---|
Attaches the current thread to the pool. |
|
Obtains the executor associated with the pool. |
|
Obtains the executor associated with the pool. |
|
Joins the threads. |
|
Notify the execution_context of a fork-related event. |
|
Stops the threads. |
|
thread_pool [constructor] |
Constructs a pool with an automatically determined number of threads.
|
Waits for threads to complete. |
|
~thread_pool [destructor] |
Destructor. |
Name |
Description |
---|---|
Destroys all services in the context. |
|
Shuts down all services in the context. |
The thread pool class is an execution context where functions are permitted to run on one of a fixed number of threads.
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.)
To submit functions to the thread pool, use the dispatch
, post
or 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();
Header: boost/asio/thread_pool.hpp
Convenience header: boost/asio.hpp