-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththread_pool.h
More file actions
112 lines (90 loc) · 2.58 KB
/
thread_pool.h
File metadata and controls
112 lines (90 loc) · 2.58 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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#pragma once
/**
* @file thread_pool.h
* @class thread_pool
* @description class thread_pool
* @author Levon Ghukasyan
*/
#include <mutex>
#include <thread>
#include <condition_variable>
#include <type_traits>
#include <future>
#include <functional>
#include <queue>
#include <vector>
class thread_pool
{
public:
/// @brief constructor
explicit thread_pool(unsigned thread_count = 0);
/// @brief destrcutor
~thread_pool();
public:
/// @brief function to shedule tasks
template <class Task, class... Args>
auto shedule(Task&& function, Args&&... args) -> std::future<typename std::result_of<Task(Args...)>::type>;
private:
bool m_stop;
std::condition_variable m_condition;
std::mutex m_mutex;
std::queue<std::function<void()> > m_tasks;
std::vector<std::thread> m_threads;
};
thread_pool::thread_pool(unsigned thread_count)
: m_stop{false}
{
if (thread_count == 0) {
thread_count = std::thread::hardware_concurrency();
}
for (unsigned i = 0; i < thread_count; ++i) {
m_threads.emplace_back([this] {
while(true) {
std::function<void()> tsk;
{
std::unique_lock<std::mutex> lock(m_mutex);
while (!m_stop && m_tasks.empty()) {
m_condition.wait(lock);
}
if (m_stop && m_tasks.empty()) {
return;
}
tsk = std::move(m_tasks.front());
m_tasks.pop();
}
tsk();
}
});
}
}
thread_pool::~thread_pool()
{
{
std::unique_lock<std::mutex> lock(m_mutex);
m_stop = true;
}
m_condition.notify_all();
for (auto& iter : m_threads) {
iter.join();
}
}
template <class Task, class... Args>
auto thread_pool::shedule(Task&& function, Args&&... args) -> std::future<typename std::result_of<Task(Args...)>::type>
{
if(m_stop) {
throw std::runtime_error("shedule on destroyed sheduler");
}
using result_type = decltype(function(args...));
auto task = std::make_shared< std::packaged_task<result_type()> >(
std::bind(std::forward<Task>(function), std::forward<Args>(args)...)
);
std::future<result_type> result(task->get_future());
{
std::unique_lock<std::mutex> lock(m_mutex);
if(m_stop)
throw std::runtime_error("enqueue on stopped ThreadPool");
m_tasks.emplace([task](){ (*task)(); });
}
m_condition.notify_one();
return result;
}