Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

thread_pool

A simple fixed-size thread pool.

class thread_pool :
  public execution_context
Types

Name

Description

executor_type

Executor used to submit functions to a thread pool.

fork_event

Fork-related event notifications.

Member Functions

Name

Description

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.

thread_pool

Constructs a pool with an automatically determined number of threads.

Constructs a pool with a specified number of threads.

~thread_pool

Destructor.

Protected Member Functions

Name

Description

destroy

Destroys all services in the context.

shutdown

Shuts down all services in the context.

Friends

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.

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

Submitting tasks to the pool

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();
Requirements

Header: boost/asio/thread_pool.hpp

Convenience header: boost/asio.hpp


PrevUpHomeNext