threadpool.hpp
is a simple header-only thread pool implementation in C++17.
No any extra dependency is required, easy to use.
#include "threadpool.hpp"
constexpr int THREADPOOL_SIZE = /* ... */;
concurrent::threadpool tp(THREADPOOL_SIZE);
template<typename Fn, typename... Args>
decltype(auto) enqueue(Fn && fn, Args &&... args);
template<typename Fn, typename... Args>
decltype(auto) enqueue_r(Fn && fn, Args &&... args);
Same as enqueue()
, yet the task will still be executed even if the thread pool is shutting down.
#include <iostream>
#include "threadpool.hpp"
int main() {
constexpr int THREADPOOL_SIZE = 2;
concurrent::threadpool tp(THREADPOOL_SIZE);
tp.enqueue_r([]() { std::cout << "hello world" << std::endl; });
return 0;
}
More checkout test.cpp
.
This implementation mainly inspired by progschj/ThreadPool and Build Your Own Threadpool With C++.
Add a new branch for C++ 17. #40 is also very helpful.
Released under BSD-2-Clause, more see LICENSE.