This project demonstrates a simple yet powerful implementation of a thread pool in C++.
- Task Queue: A thread-safe queue to hold pending tasks.
- Worker Threads: A fixed number of threads that consume tasks from the queue.
- Synchronization Primitives: Mutexes and condition variables for efficient coordination.
- Future-based Results: Retrieve results of tasks asynchronously using
std::future. - Graceful Shutdown: Ensures all tasks are completed before shutting down the thread pool.
ThreadPool.h: Contains the declaration of theThreadPoolclass.ThreadPool.cpp: Contains the implementation of theThreadPoolclass.main.cpp: Demonstrates the usage of the thread pool with an example.
#include "ThreadPool.h"
#include <iostream>
#include <vector>
#include <chrono>
int main() {
ThreadPool pool(4); // Create a thread pool with 4 worker threads
std::vector<std::future<int>> results;
for (int i = 0; i < 8; ++i) {
results.emplace_back(
pool.enqueue([i] {
std::this_thread::sleep_for(std::chrono::seconds(1));
return i * i;
})
);
}
for (auto& result : results) {
std::cout << result.get() << " ";
}
std::cout << std::endl;
return 0;
}-
Ensure you have a C++ compiler that supports C++17 or later (e.g.,
g++orclang++). -
Compile the code using the following command:
g++ *.cpp -o threadpool -std=c++17 -
Run the compiled executable:
./threadpool
The program calculates the squares of numbers from 0 to 7 using the thread pool. The output will look like this (order may vary due to concurrency):
0 1 4 9 16 25 36 49
- Thread Count: Use
std::thread::hardware_concurrency()to determine the optimal number of threads for CPU-bound tasks. - Lock Contention: Minimized by holding the mutex only while accessing the task queue.
- Exception Safety: Tasks are executed in a try-catch block to prevent unhandled exceptions.
This project is open-source and available under the MIT License