This is a header file only implementation of a C++20 style thread pool.
Compilation has been tested with:
- GCC 11.3.0 (GNU/Linux Ubuntu 22.04.1 LTS)
- cmake 3.22.1
- googletest 1.11.0-3
- clang 14.0.0-1ubuntu1
- Visual Studio Community 2022 17.4.4 (64-bit) (Windows 11)
- cmake 3.26.0-rc1
- googletest 1.13.0
Create a thread pool and add jobs to it to be performed by the running threads.
void printVal(void)
{
static int val = 1;
std::cout << "Thread job: " << val++ << std::endl;
}
auto main(int argc, char* argv[]) -> int
{
com::thread::Pool p(1);
for (auto i = 0; i < 5; ++i)
{
p.add(printVal);
}
...
}
All methods of the thread pool are thread safe.
Use the ThreadPool.hpp
file in your source tree and include it in the file that need to use it.
Unit tests run with ctest:
ctest -VV -C debug
All contributions are highly appreciated.