This project implements a thread pool using a worker-based design pattern with a channel for task management and inter-thread communication. It is designed for efficient task execution in a multithreaded environment, featuring:
- A thread-safe Channel for task queuing and communication.
- A Worker class to process tasks.
- A ThreadPool class to manage multiple workers and distribute tasks.
- Worker Management: Automatically starts and manages workers in a pool.
- Thread-Safe Channel: Implements a thread-safe queue using condition variables and mutexes.
- Dynamic Task Assignment: Supports submitting tasks to the pool and redistributing workloads.
- Monitoring and Recovery:
- Detects deadlocks.
- Monitors worker heartbeats.
- Restarts workers if they become unresponsive.
- Channel
A generic, thread-safe channel for task queuing and communication. - Worker
A worker thread that executes tasks from its queue. - ThreadPool
A thread pool that manages multiple workers and distributes tasks.
send
andreceive
: For pushing tasks into and retrieving tasks from the channel.close
: To stop accepting new tasks.
start
,stop
, andjoin
: For lifecycle management of the worker thread.submit
: For submitting tasks to the worker's queue.
submit
: For submitting tasks to the pool.shutdown
: Gracefully stops all workers.restart_worker
: Restarts a specific worker in case of failure.
- A C++17 or newer compiler (e.g., GCC, Clang).
- CMake (version 3.12 or higher).
git clone https://github.com/lazy-cat-y/cpp-thread-pool.git
cd cpp-thread-pool
This project supports configurable build options via CMake
. You can customize the build by enabling or disabling specific features.
Option Name | Description | Default Value |
---|---|---|
ENABLE_TEST |
Enable building unit tests using Google Test. | OFF |
CMAKE_BUILD_TYPE |
Specify the build type. Options: Debug , Release , RelWithDebInfo , MinSizeRel . |
Release |
You can configure these options by passing them as arguments to the cmake
command.
If you want to build the project without tests:
cmake -DENABLE_TEST=OFF ..
If you want to build the project with optimizations:
cmake -DCMAKE_BUILD_TYPE=Release ..
To enable tests and set the build type to Debug
:
cmake -DENABLE_TEST=ON -DCMAKE_BUILD_TYPE=Debug ..
Once CMake is configured, you can verify the applied options in the generated CMakeCache.txt
file in your build directory. Look for lines like these:
ENABLE_TEST:BOOL=OFF
CMAKE_BUILD_TYPE:STRING=Release
Here's an example of how to use the thread pool:
#include "worker-pool.h"
#include <iostream>
int main() {
ThreadPool<4, 10, 2> pool;
std::vector<std::future<int>> futures;
for (int i = 0; i < 10; ++i) {
futures.push_back(pool.submit([](int x) { return x * x; }, i));
}
for (int i = 0; i < 10; ++i) {
futures[i].get();
}
pool.shutdown();
}
.
├── CMakeLists.txt # Build configuration
├── src
│ ├── include
│ │ ├── channel.hpp # Channel implementation
│ │ ├── m-define.h # Macros for thread pool
│ │ ├── worker.hpp # Worker implementation
│ │ └── worker-pool.hpp # ThreadPool implementation
│ └── CMakelists.txt # Source files
├── tests
│ ├── test-channel.cc # Unit tests for the channel
│ ├── test-pool.cc # Unit tests for the thread pool
│ └── test-worker.c # Unit tests for the workers
├── third-party
│ └── ... # Dependencies
├── README.md
└── LICENSE # License file (MIT)
Contributions are welcome! Please follow these steps:
- Fork this repository.
- Create a new branch:
git checkout -b feature-name
- Commit your changes:
git commit -m "Add feature-name"
- Push to your branch:
git push origin feature-name
- Open a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.
- Haoxin Yang
GitHub: lazy-cat-y