-
Notifications
You must be signed in to change notification settings - Fork 43
/
threadpool.h
81 lines (70 loc) · 2.14 KB
/
threadpool.h
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
// encode UTF-8
// @Author : Aged_cat
// @Date : 2021-05-04
#ifndef THREADPOOL_H
#define THREADPOOL_H
#include<thread>
#include<condition_variable>
#include<mutex>
#include<vector>
#include<queue>
#include<future>
class ThreadPool{
private:
bool m_stop;
std::vector<std::thread>m_thread;
std::queue<std::function<void()>>tasks;
std::mutex m_mutex;
std::condition_variable m_cv;
public:
explicit ThreadPool(size_t threadNumber):m_stop(false){
for(size_t i=0;i<threadNumber;++i)
{
m_thread.emplace_back(
[this](){
for(;;)
{
std::function<void()>task;
{
std::unique_lock<std::mutex>lk(m_mutex);
m_cv.wait(lk,[this](){ return m_stop||!tasks.empty();});
if(m_stop&&tasks.empty()) return;
task=std::move(tasks.front());
tasks.pop();
}
task();
}
}
);
}
}
ThreadPool(const ThreadPool &) = delete;
ThreadPool(ThreadPool &&) = delete;
ThreadPool & operator=(const ThreadPool &) = delete;
ThreadPool & operator=(ThreadPool &&) = delete;
~ThreadPool(){
{
std::unique_lock<std::mutex>lk(m_mutex);
m_stop=true;
}
m_cv.notify_all();
for(auto& threads:m_thread)
{
threads.join();
}
}
template<typename F,typename... Args>
auto submit(F&& f,Args&&... args)->std::future<decltype(f(args...))>{
auto taskPtr=std::make_shared<std::packaged_task<decltype(f(args...))()>>(
std::bind(std::forward<F>(f),std::forward<Args>(args)...)
);
{
std::unique_lock<std::mutex>lk(m_mutex);
if(m_stop) throw std::runtime_error("submit on stopped ThreadPool");
tasks.emplace([taskPtr](){ (*taskPtr)(); });
}
m_cv.notify_one();
return taskPtr->get_future();
}
};
#endif //THREADPOOL_H