-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreadPool.cpp
54 lines (46 loc) · 1.05 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
//
// Created by Federico Gianno on 27/09/2018.
//
#include <iostream>
#include "ThreadPool.h"
void ThreadPool::startup()
{
try {
call_once(on, [this](){
for(int i=0; i<nthreads; i++)
threads.emplace_back(thread{&ThreadPool::worker, this});
});
}
catch(int e) {
cout << "startup() just called once. Exception: " << e << endl;
}
}
void ThreadPool::shutdown()
{
try {
call_once(off, [this](){
i.invalidate();
for (auto& th : threads)
th.join();
});
}
catch(int e) {
cout << "shutdown() just called once. Exception: " << e << endl;
}
}
void ThreadPool::worker()
{
bool keeprunning = true;
int var;
while(keeprunning){
keeprunning = i.pop(var); //pop value and put it in var, if queue is invalid then keeprunning will be set to false
if(keeprunning){
//do stuff
cout << "Pop value: " << var << endl;
}
}
}
void ThreadPool::post_task(int var)
{
i.push(var);
}