ThreadPoolManager is a C++ project that implements an efficient multi-threading system using a thread pool for generic functions of the same type and different tasks. It includes task management, synchronization mechanisms, and thread-safe logging to demonstrate concurrent task execution.
- Efficient thread pooling
- Task queue management
- Synchronization mechanisms with mutexes and condition variables
- Thread-safe logging system
- Exception handling within tasks
- C++20 or later
- A compatible compiler (MinGW G++) or Microsoft Visual C++ (MSVC)
- CPU with more than 2 Threads
-
Clone the repository:
git clone https://github.com/yourusername/ThreadPoolManager.git cd ThreadPoolManager
-
Build the project using CMake:
mkdir build cd build cmake .. make
Contributions are welcome! Please fork the repository and submit a pull request for any improvements or bug fixes.
This project is licensed under the GNU General Public License (GNU GPL). - see the LICENSE file for details.
email: farsi.masoud@gmail.com Linkedin: https://www.linkedin.com/in/bardiaparsi/ GitHub: https://github.com/BardiParsi
-
Run the executable:
./ThreadPoolManager
-
Modify the
main.cpp
file to change the tasks or the number of threads.
- src/: Source files
- main.cpp: Entry point of the application
- ThreadPool.cpp: Implementation of the ThreadPool class
- HeavyTask.cpp: Implementation of the HeavyTask class
- Logger.cpp: Implementation of the Logger class
- include/: Header files
- ThreadPool.h: Header for the ThreadPool class
- HeavyTask.h: Header for the HeavyTask class
- Logger.h: Header for the Logger class
- CMakeLists.txt: CMake build configuration
Here is a simple example to demonstrate the usage:
#include <iostream>
#include "ThreadPool.h"
#include "Logger.h"
void taskSimulator(int id) {
std::ostringstream oss;
oss << std::this_thread::get_id();
Logger::INFO("Task " + std::to_string(id) + " running on thread ID: " + oss.str() + "\n");
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
int main() {
const int CPUThreadNum = std::thread::hardware_concurrency();
int numberOfThreads {CPUThreadNum};
assert(numberOfThreads >= 2);
if (numberOfThreads >= 2) {
ThreadPool pool(numberOfThreads);
for (int i = 0; i < numberOfThreads; i++) {
pool.enqueueTask([i](){ taskSimulator(i); });
}
} else {
Logger::INFO("THERE ARE NOT ENOUGH CPU THREADS ON THIS DEVICE");
}
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
std::cin.get();
return 0;
}