C++ thread pool library implementation.
git clone --recursive https://github.com/rsitko92/thread-pool.git
-
with using Docker:
-
without using Docker:
- GCC with C++17 standard support
- CMake >= 3.7
- Git
- pthread library
- GCC with C++17 standard support
- CMake >= 3.7
- Git
- pthread library
Being in root directory of project run in terminal:
sudo test/buildAndRunTestsDocker.sh
Being in root directory of project run in terminal:
test/buildAndRunTests.sh
-
Add thread pool project using ExternalProject_Add function in CMakeLists.txt file.
-
Link
libThreadPool.a
static library to project target in CMakeLists.txt file. -
Include
DefaultFixedThreadPool.hpp
header file frominc
directory in header/source file:#include "DefaultFixedThreadPool.hpp"
-
Create thread pool instance:
auto pThreadPool = thread_pool::DefaultFixedThreadPool::create();
-
Enqueue task in thread pool:
auto exampleTask = [](int x) {std::this_thread::sleep_for(std::chrono::seconds(2)); std::cout << "Hello from worker thread" << std::endl; std::cout << "Passed argument value = " << x << std::endl;}; auto future = pThreadPool->enqueue(exampleTask, 10);
Notice 1: In this example also
chrono
andiostream
headers must be included.Notice 2: You can enqueue tasks and its arguments as rvalue objects. Then there will be invoked move constructors if exists for these class of objects.
-
Get result:
future.get();