This repository contains a straightforward implementation of a thread pool using pure C++20 standard library features. The thread pool allows for efficient management and execution of multiple concurrent tasks, making it a valuable tool for boosting the performance of multithreaded applications.
- Modern C++20: Utilizes the latest features of C++20 for enhanced performance and simplicity.
- Dynamic Task Management: Easily add tasks to the pool and have them executed by available threads.
- Thread Safety: Ensures safe concurrent execution of tasks.
- Scalable: Automatically manages the number of threads based on the system's capabilities.
- A C++20 compatible compiler (e.g., GCC 10+, Clang 10+, MSVC 2019+)
- CMake (optional, for building examples and tests)
To build the project, you can use the following CMake commands:
cmake -B build && cmake --build build && sudo cmake --install buildor, step-by-step:
cmake -B buildcmake --build buildsudo cmake --install buildHere is a basic example of how to use the thread pool:
#include <threadpool-cpp/threadpool-cpp.h>
#include <thread>
#include <iostream>
int main(int, char*[]) {
using namespace tp = threadpoolcpp;
int threads = std::thread::hardware_concurrency() ? std::thread::hardware_concurrency() : 4;
auto tp::pool(threads); // Create a thread pool with hardware concurrency (or 4 by default) threads
// Add a task to the thread pool
auto result = pool.enqueue([] {
return "Hello from the thread pool!";
});
std::cout << result.get() << std::endl; // Prints: Hello from the thread pool!
return 0;
}- Code Documentation: Inline comments and documentation are provided within the source code.
- Examples: Check the examples directory for more usage examples.
We welcome contributions! Please fork the repository and submit pull requests.
This project is licensed under the MIT License. See the LICENSE file for more details.