10 boost::asio::thread_pool

boost asio provides a thread pool. The following code shows how it can be used:


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

This raises some questions:

  • Does it make sense to have both an IoContextPool and a boost::asio::thread_pool? That will increase the number of threads, but it is still reasonable.
  • It would be nice to know more about how boost::asio::thread_pool is implemented. How many associated contexts are there? Is there a queue of tasks for each thread? Is there work stealing? How efficient is it?

If we use IoContextPool as a thread pool then presumably we call get_io_context() to obtain an io_context to use (these are provided round robin from the pool), and then we use boost::asio::post to post the task to the io_context. That's rather like each thread having it's own queue, but there is no work stealing.

Decision: We shall only use an IoContextPool as a thread pool, and typically we call get_io_context() to get a context then post a task to it using boost::asio::post. However, in some cases we shall tend to reuse a given io_context. For example, the LazyFlusher can use the same io_context over its entire life.