-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadPool.hpp
More file actions
43 lines (35 loc) · 1.28 KB
/
ThreadPool.hpp
File metadata and controls
43 lines (35 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#ifndef THREADPOOL_HPP
#define THREADPOOL_HPP
#include <vector>
#include <thread>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <atomic>
class ThreadPool
{
public:
ThreadPool(); // Default constructor: Initialize with available threads
ThreadPool(size_t num_threads);
~ThreadPool();
void submitTask(std::function<void()> task);
void submitPrintTask(std::function<void()> task);
void shutdown();
int getWorkerCount();
private:
void worker_loop(); // Worker thread loop
std::vector<std::thread> workers; // Pool of worker threads
std::queue<std::function<void()>> tasks; // Queue of tasks to be executed
std::mutex queue_mutex; // Mutex for thread synchronization
std::condition_variable condition; // Condition variable to manage task execution
std::atomic<bool> taskStop = false; // Flag to stop the threads when shutting down
void printer_loop(); // Function for the printing thread
std::thread printerThread;
std::queue<std::function<void()>> printQueue; // Queue for messages to print
std::mutex printQueueMutex;
std::condition_variable printCondition;
std::atomic<bool> printStop = false;
bool verbose = false; // Flag to enable verbose output
};
#endif // THREADPOOL_HPP