-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpool.cpp
More file actions
41 lines (34 loc) · 950 Bytes
/
pool.cpp
File metadata and controls
41 lines (34 loc) · 950 Bytes
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
#include <nexus/pool.h>
nexus::pool::pool(size_t numThreads) : isRunning(true) {
logger::success("Running nexus with " + std::to_string(numThreads) +
" threads");
for (size_t i = 0; i < numThreads; i++) {
this->workers.emplace_back([this] {
while (true) {
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(this->mutex);
this->condition.wait(lock, [this] {
return !this->isRunning || !this->tasks.empty();
});
if (!this->isRunning && this->tasks.empty()) {
return;
}
task = std::move(this->tasks.front());
this->tasks.pop();
}
task();
}
});
}
}
nexus::pool::~pool() {
{
std::unique_lock<std::mutex> lock(this->mutex);
this->isRunning = false;
}
this->condition.notify_all();
for (std::thread &worker : this->workers) {
worker.join();
}
}