42
votes

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.

7
What is the problem with boost?David Rodríguez - dribeas
@David - there is no built-in threadpool in Boost, is there?Steve Townsend
@Steve Townsend: Right, sorry... I thought I remember one in boost, but it is not really included (has not been accepted). There is one available in threadpool.sourceforge.net/index.htmlDavid Rodríguez - dribeas
This FOSS project is my attempt to create a thread pool library, check it out if you want. -> code.google.com/p/threadpool11Etherealone

7 Answers

21
votes

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.

9
votes

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 Standard Template Library. For example, fifo container from lock free library.

Good luck!

7
votes

I've written a small example here. Basically what you need to do is to implement this piece of code:

asio::io_service io_service;
boost::thread_group threads;
auto_ptr<asio::io_service::work> work(new asio::io_service::work(io_service)); 

// Spawn enough worker threads
int cores_number = boost::thread::hardware_concurrency();
for (std::size_t i = 0; i < cores_number; ++i){
    threads.create_thread(boost::bind(&asio::io_service::run, &io_service));
}
// Post the tasks to the io_service
for(vector<string>::iterator it=tasks.begin();it!=tasks.end();it++){
   io_service.dispatch(/* YOUR operator()() here */);
}
work.reset();
3
votes

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.

1
votes

Here is a simple header-only task queue using a thread pool (built on Boost): taskqueue.hpp

The TaskQueue project page includes a sample application demonstrating how to use it:

0
votes

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.

0
votes

An example implementation using the ffead-cpp framework is described here. It provides direct, priority-based as well as a scheduled thread pool implementation. Check it out...