-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadPool.cpp
More file actions
150 lines (128 loc) · 3.05 KB
/
ThreadPool.cpp
File metadata and controls
150 lines (128 loc) · 3.05 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "ThreadPool.hpp"
#include <iostream>
ThreadPool::ThreadPool()
{
int num_threads = std::thread::hardware_concurrency(); // Get the number of available threads
for (size_t i = 0; i < num_threads; ++i)
{
workers.emplace_back(&ThreadPool::worker_loop, this);
}
// Start the printing thread
printerThread = std::thread(&ThreadPool::printer_loop, this);
}
ThreadPool::ThreadPool(size_t num_threads)
{
for (size_t i = 0; i < num_threads; ++i)
{
workers.emplace_back(&ThreadPool::worker_loop, this);
}
// Start the printing thread
printerThread = std::thread(&ThreadPool::printer_loop, this);
}
void ThreadPool::worker_loop()
{
while (true)
{
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(queue_mutex);
condition.wait(lock, [&]() {
return taskStop || !tasks.empty();
});
if (taskStop && tasks.empty()) break;
task = std::move(tasks.front());
tasks.pop();
}
task();
}
}
void ThreadPool::printer_loop()
{
while (true)
{
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(printQueueMutex);
printCondition.wait(lock, [&]() {
return printStop || !printQueue.empty();
});
if (verbose)
{
std::cout << "printStop: " << printStop << ", printQueue.size(): " << printQueue.size() << "\n";
}
if (printStop && printQueue.empty())
{
if (verbose)
{
std::cout << "ThreadPool: Printer thread stopping.\n";
}
break;
}
if (!printQueue.empty())
{
task = std::move(printQueue.front());
printQueue.pop();
if (verbose)
{
std::cout << "ThreadPool: Task retrieved from printQueue. Remaining size: " << printQueue.size() << "\n";
}
}
}
task();
}
}
void ThreadPool::submitTask(std::function<void()> task)
{
{
std::unique_lock<std::mutex> lock(queue_mutex);
tasks.push(std::move(task));
}
condition.notify_one();
}
void ThreadPool::submitPrintTask(std::function<void()> task) {
{
std::unique_lock<std::mutex> lock(printQueueMutex);
printQueue.push(task);
if (verbose)
{
std::cout << "ThreadPool: Task added to printQueue. Queue size: " << printQueue.size() << "\n";
}
}
printCondition.notify_one();
}
void ThreadPool::shutdown()
{
{
std::unique_lock<std::mutex> lock(queue_mutex);
taskStop = true;
}
condition.notify_all();
// Wait for all threads to finish
for (std::thread &worker : workers)
{
if (worker.joinable())
{
worker.join();
}
}
// Signal the printer thread to stop after all tasks are submitted
{
std::unique_lock<std::mutex> lock(printQueueMutex);
printStop = true; // Signal the printer thread to stop
}
printCondition.notify_one();
// Wait for the printing thread to finish
if (printerThread.joinable())
{
printerThread.join();
}
}
ThreadPool::~ThreadPool()
{
shutdown();
}
//getters
int ThreadPool::getWorkerCount()
{
return workers.size();
}