views:

158

answers:

5
+5  Q: 

C++ Thread Pool

What is a good open source implementation of a thread pool for C++ to use in production code (something like boost)?

Please provide either your own example code or a link to example code usage.

A: 

You might want to look at http://threadpool.sourceforge.net/

It is not hard to implement thread pool yourself using Boost.Thread. Depending on the task, you might want to use lock-free container for the queue instead of one from STL. For example, fifo container from lock free library.

Good luck!

Vlad Lazarenko
A: 

This library builds on Boost.Thread. There is a short tutorial with some example code. If this does not do what you want, you could use it as a baseline.

Make sure you are on a Boost version >= 1.37 if you go this route.

Steve Townsend
+3  A: 

I think it is still not accepted into Boost, but a good staring point: threadpool. Some example of usage, from the web site:

#include "threadpool.hpp"

using namespace boost::threadpool;

// Some example tasks
void first_task()
{
  ...
}

void second_task()
{
  ...
}

void third_task()
{
  ...
}

void execute_with_threadpool()
{
  // Create a thread pool.
  pool tp(2);

  // Add some tasks to the pool.
  tp.schedule(&first_task);
  tp.schedule(&second_task);
  tp.schedule(&third_task);

  // Leave this function and wait until all tasks are finished.
}

The argument "2" to the pool indicates the number of threads. In this case, the destruction of tp waits for all threads to finish.

Diego Sevilla
What is the meaning of `2` in the statement `pool tp(2);`?
ArunSaha
@ArunSaha: Indicates the number of initial threads. I'll add it to the answer.
Diego Sevilla
A: 

I believe you can emulate a thread pool with an io_service in boost::asio. You can control the number of threads available to the io_service pool, and then you can "post" tasks to the io_service, which will get executed by one of the threads in the pool. Each such task has to be a functor (I believe).

I can't put an example here right now, but the asio documentation on io_service pools will outline how this can be done.

Nim