-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreadpool.cpp
71 lines (56 loc) · 1.55 KB
/
threadpool.cpp
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
#include "threadpool.h"
using namespace std;
ThreadPool::ThreadPool() {
}
ThreadPool::ThreadPool(int nbrThreads) {
/* Create a thread pool with nbrThreads threads
*/
shutdown = false;
threads.reserve(nbrThreads);
for (int i = 0; i < nbrThreads; i++) {
threads.emplace_back(std::bind(&ThreadPool::entry, this, i));
}
}
ThreadPool::~ThreadPool() {
/* Wait until all threads have finished their jobs.
* When finished, delete all threads.
*/
{
unique_lock <std::mutex> loc(queueLock);
shutdown = true;
}
condVar.notify_all();
for (auto & thread : threads) {
thread.join(); //wait all threads
}
}
void ThreadPool::addJob(function <void(void)> func) {
/* Add a job to the queue
*/
{
unique_lock <mutex> lock(queueLock); //lock the jobs vector
jobs.push_back(func);
}
condVar.notify_one();
}
void ThreadPool::entry(int i) {
/*
* Main fonction for the threads. This is where they are executing their jobs
* Infinite loop until there is no jobs or we want to shutdown
*/
function <void (void)> job;
while(true) {
{
unique_lock<mutex> lock(queueLock); //lock the jobs vector
if (shutdown && jobs.empty()) {
return ; //if no job available and we want to shutdown : leave the infinite loop
}
while (!shutdown && jobs.empty()) { //if no jobs available and it doesn't have to shutdown -> wait
condVar.wait(lock);
}
job = jobs.front(); //pick the first job
jobs.pop_front(); //remove the first job from vector
} //unlock the jobs vector
job(); //all locks are removed when doing the job
}
}