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.
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).
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).
ThreadPool is an easy-to-use header-only thread pool library using C++11 threading API, and no GPL.
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.