-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadPool.cpp
More file actions
58 lines (42 loc) · 1.2 KB
/
ThreadPool.cpp
File metadata and controls
58 lines (42 loc) · 1.2 KB
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
#include "ThreadPool.h"
ThreadPool::ThreadPool(size_t threadnum, const std::string& threadtype):m_stop(false), m_threadtype(threadtype){
for (int i = 0; i < threadnum; ++i) {
m_threads.emplace_back([this](){
printf("create %s thread(%d).\n", m_threadtype.c_str(), syscall(SYS_gettid));
while(m_stop == false) {
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(this->m_mutex);
m_condition.wait(lock, [this]{
return this->m_stop == true || this->m_taskqueue.empty() == false;
});
if (this->m_stop == true && this->m_taskqueue.empty() == true) return;
task = std::move(this->m_taskqueue.front());
this->m_taskqueue.pop();
}
task();
}
});
}
}
size_t ThreadPool::size() const{
return m_threads.size();
}
void ThreadPool::stop() {
if (m_stop) return;
m_stop = true;
m_condition.notify_all();
for (auto& thread:m_threads) {
thread.join();
}
}
ThreadPool::~ThreadPool() {
stop();
}
void ThreadPool::addtask(std::function<void()> task) {
{
std::unique_lock<std::mutex> lock(m_mutex);
m_taskqueue.push(task);
}
m_condition.notify_one();
}