5

I am looking for a thread pool implementation based on C++11 threading facilities. The library should be preferably headers only. License has to be BSD style or MIT style permissive kind of license, i.e., no GPL.

4

There seems to be a GitHub of someone who developed a small library for C++ 11.

It also is just a central header file (no cpp files to add), and fits into your License requirements (it actually uses a permissive free software license).

| improve this answer | |
3

I noticed this question trying to ask the same thing basically, except that I've already noticed a few, and I would like to get a proper recommendation/review of strengths and weaknesses rather than just a list. But for now, here's my list:

  • CTPL - A no-frills single-header-only library; has two variants: one only relying on the standard library, the other using a lock-free queue implementation from Boost. ~250 lines lines at this time.
  • threadpool11 by Tolga Hoşgör - "A fast, lock-free, cross-platform, full CPU utilizing thread pool implementation using C++11 features." claims to beat OpenMP in some benchmark; at the very least it means the author is proud and confident about it rather than just putting it out there... ~590 lines but that's with a lot more boilerplate license etc. So probably something like 500 lines.
  • thread_pool by Tyler Hardin - A single-class, 1 .hpp + 1 .cpp, library (the choice of code to put in the .cpp is very reasonable, so this is not a weakness compared to CTPL). ~290 lines
  • A Boost.ASIO-based 30-line threadpool implementation...

Now, I have not used any of these and cannot describe the differences between them, but would very much like someone with experience using threadpools to do so (in a separate answer).

| improve this answer | |
  • So, I saw this in my referrer list on github. My library has changed a bit. It's still possible to use as a one header, one source lib, but it also has cooperative threading with a priority queue on POSIX now. This is really useful for solving recursive problems with a thread pool. Without yielding, it's impossible to avoid blocking while waiting on the results of subcomputations. – Tyler Dec 11 '16 at 1:21
  • Oh, and it's also not C++11 compatible anymore. The last C++11 and 14 compatible versions are documented in the readme, though, so with a bit of git competence it's possible to get and use them. – Tyler Dec 11 '16 at 1:23
  • @Tyler: Then, can you please edit my answer to reflect the state of your library right now? Alternatively, post your own, longer answer, describing the strengths and weaknesses of your library. – einpoklum Dec 11 '16 at 7:31
  • Note that those 30 lines about Boost.ASIO implementation is not quite fair (for comparison), since it uses other Boost facilities such as boost::asio::io_service::work, boost::asio::io_service, and boost::thread_group. – Daniel Langr Apr 21 at 7:31
  • @DanielLangr: You are quite welcome to edit the answer and expand the the last bullet... – einpoklum Apr 21 at 8:19
1

ThreadPool

ThreadPool is an easy-to-use header-only thread pool library using C++11 threading API, and no GPL.

| improve this answer | |
1

I am looking for a thread pool implementation based on C++11 threading facilities.

This is kind-of generic description. Basically, there are at least two types of thread pools. First, and much much simpler ones, allow you just to enqueue some jobs (tasks) represented by function objects and assign these tasks to running threads, which run these functors until they finish. If I am not wrong, all the recommended solutions work this way.

However, the main drawback of these simple solutions is that they are not able to suspend and resume tasks. This is anything but easy, and it is provided, e.g., by OpenMP or TBB. Consider the most simple example of using tasks for calculation of Fibonacci number with OpenMP:

int Fib(int n)
{
   if (n < 2) return n;
   int x, y;
   #pragma omp task shared(x)
   x = Fib(n - 1);
   #pragma omp task shared(y)
   y = Fib(n - 2);
   #pragma omp taskwait
   return x + y;
}

In this case, Fib function is suspended at the #pragma omp taskwait line and resumed (optionally even on different thread) after children tasks (tasks created in the task) are finished. You therefore need some mechanism to "jump out" of the function and "jump in" again (will coroutines from C++20 help?).

As for pure C++, I found two thread pool implementations that are a bit more complex:

  • cpp-taskflow — seems to be header only. There is what they call a dynamic tasking capability, which according to examples, allow creating new tasks within running tasks. But I cannot find the option to wait for the completion of children tasks.

  • dougbinks/enkiTS — not header only. There is some waiting mechanism, but I think it's "blocking" in the sense that the thread that executes the waiting tasks is not albe to execute another tasks during waiting.

However, I found these libraries by looking for regular tasking opitons in modern C++ and need to admit that I don't know much about them. It would be nice if anyone could provide more details.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.