-
Notifications
You must be signed in to change notification settings - Fork 0
/
threadpool.cpp
79 lines (74 loc) · 1.66 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
72
73
74
75
76
77
78
79
#include <iostream>
#include <mutex>
#include <condition_variable>
#include <unistd.h>
#include <map>
#include <algorithm>
#include <utility>
#include <thread>
#include <typeindex>
#include <queue>
#include "threadpool.h"
void Taskmap::insert_task(int priority, std::function<void()> func)
{
std::lock_guard<std::mutex> guard(mut);
m.insert(std::make_pair(priority, func));
}
bool Taskmap::empty()
{
std::lock_guard<std::mutex> guard(mut);
return m.empty();
}
std::function<void()> Taskmap::pull_task()
{
std::lock_guard<std::mutex> guard(mut);
auto it = m.rbegin();
if (it == m.rend()) return nullptr;
auto task = it->second;
m.erase(std::next(it).base());
return task ;
}
Threadpool::Threadpool(int max_worker) : quit(false)
{
for(int i=0; i<max_worker;i++) {
thread_deque.emplace_back(
[this, i]
{
while(1)
{
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(this->mut);
this->cv.wait(lock,[this]{ return this->quit || !this->tm.empty(); });
if(this->quit && this->tm.empty()) return;
task = this->tm.pull_task();
}
task();
}
}
);
}
}
Threadpool::~Threadpool()
{
quit = true;
cv.notify_all();
while(!thread_deque.empty()) {
(thread_deque.front()).join();
thread_deque.pop_front();
}
}
void Threadpool::end_pool()
{
std::lock_guard<std::mutex> guard(mut);
quit = true;
cv.notify_all();
}
template<class F, class... Args> void Threadpool::queue_task(int pri, F&& f, Args&&... args)
{
{
std::lock_guard<std::mutex> guard(mut);
tm.insert_task(pri, std::function<void()>(f));
}
cv.notify_one();
}