-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreadPool.hpp
121 lines (89 loc) · 2.28 KB
/
ThreadPool.hpp
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
/*
* ThreadPool.hpp
*
* Created on: May 24, 2020
* Author: voukatas
*/
#ifndef THREADPOOL_HPP_
#define THREADPOOL_HPP_
#define DEBUG 1
#include <functional>
#include <vector>
#include <thread>
#include <queue>
#include <condition_variable>
#include <iostream>
class ThreadPool{
using Job = std::function<void()>;
private:
//keep track of threads for cleanup
std::vector<std::thread> workers;
std::queue<Job> jobQ;
std::mutex jobQMutex;
std::condition_variable jobQCv;
bool stopExecution = false;
public:
ThreadPool(std::size_t numOfWorkers){
start(numOfWorkers);
}
~ThreadPool(){
shutdown();
}
void addJob(Job job){
//create local scope for lock and avoid race conditions
{
std::unique_lock<std::mutex> lock{jobQMutex};
//emplace creates a std::function<void()> and stores it in our queue
jobQ.emplace(job);
}
//jobQ has a job so notify a thread
jobQCv.notify_one();
}
private:
void start(std::size_t numOfWorkers){
for(std::size_t i = 0; i < numOfWorkers; i++){
//create and hold worker threads
workers.emplace_back([=](){
std::thread::id worker_id;//only for debugging
if(DEBUG){
worker_id = std::this_thread::get_id();
}
while(true){
Job job;
//critical section below
//use a lock to protect the jobQ and a cv to avoid unnecessary cpu utilization
{
std::unique_lock<std::mutex> lock{jobQMutex};
jobQCv.wait(lock, [=] { return !jobQ.empty() || stopExecution; });
if( jobQ.empty() && stopExecution ){
break;
}
//not sure if move or copy is faster,move needs to move the callable and empty the moved-from object.. needs further investigation
//https://stackoverflow.com/questions/13680587/move-semantic-with-stdfunction
job = std::move(jobQ.front());
jobQ.pop();
}
//better execute the job out of scope in order to hold the lock less
job();
if(DEBUG){
std::cout<<"Thread:"<<worker_id<<" finished"<<std::endl;
}
}
});
}
}
void shutdown(){
if(DEBUG){
//std::cout<<"Shutting down!!!!"<<std::endl;
}
{
std::unique_lock<std::mutex> lock(jobQMutex);
stopExecution = true;
}
jobQCv.notify_all();
for(std::thread& worker: workers){
worker.join();
}
}
};
#endif /* THREADPOOL_HPP_ */