forked from heavywatal/cxxwtl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcurrent.hpp
168 lines (145 loc) · 4.75 KB
/
concurrent.hpp
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#pragma once
#ifndef WTL_CONCURRENT_HPP_
#define WTL_CONCURRENT_HPP_
#include <thread>
#include <mutex>
#include <condition_variable>
#include <future>
#include <chrono>
#include <vector>
#include <queue>
#include <memory>
#include <type_traits>
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////
namespace wtl {
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////
// compatible with std::lock_guard<BasicLockable>
class Semaphore {
public:
explicit
Semaphore(unsigned int n=std::thread::hardware_concurrency()) noexcept:
count_(n) {}
void lock() {
std::unique_lock<std::mutex> lck(mutex_);
condition_.wait(lck, [this]{return count_ > 0;});
--count_;
}
void unlock() {
std::lock_guard<std::mutex> lck(mutex_);
++count_;
condition_.notify_one();
}
// Pythonic alias
void acquire() {lock();}
void release() {unlock();}
private:
std::mutex mutex_;
std::condition_variable condition_;
unsigned int count_;
};
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////
template <typename T> inline
std::future_status status(const std::future<T>& future) {
return future.wait_for(std::chrono::seconds(0));
}
template <typename T> inline
bool is_ready(const std::future<T>& future) {
return status(future) == std::future_status::ready;
}
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////
class BasicTask {
public:
BasicTask() noexcept = default;
BasicTask(BasicTask&&) noexcept = default;
BasicTask(const BasicTask&) = delete;
virtual ~BasicTask() = default;
virtual void operator()() = 0;
};
template <typename result_t>
class Task: public BasicTask {
public:
template <class Func>
Task(Func&& func) noexcept: std_task_(std::forward<Func>(func)) {}
std::future<result_t> get_future() {return std_task_.get_future();}
void operator()() override {std_task_();}
private:
std::packaged_task<result_t()> std_task_;
};
static_assert(!std::is_default_constructible<Task<void>>{}, "");
static_assert(!std::is_copy_constructible<Task<void>>{}, "");
static_assert(std::is_nothrow_move_constructible<Task<void>>{}, "");
class ThreadPool {
public:
ThreadPool(unsigned int n) {
for (unsigned int i=0; i<n; ++i) {
threads_.emplace_back(&ThreadPool::run, this);
}
}
~ThreadPool() {
is_being_destroyed_ = true;
condition_run_.notify_all();
for (auto& th: threads_) {
th.join();
}
}
template <class Func>
void submit(Func&& func) {
std::lock_guard<std::mutex> lck(mutex_);
tasks_.push(std::make_unique<Task<void>>(std::forward<Func>(func)));
condition_run_.notify_one();
}
template <class Func, class... Args>
auto submit(Func&& func, Args&&... args) {
#if __cplusplus >= 201703L
using result_t = std::invoke_result_t<Func, Args...>;
#else
using result_t = std::result_of_t<Func(Args...)>;
#endif
std::lock_guard<std::mutex> lck(mutex_);
auto task = std::make_unique<Task<result_t>>(
[&func, args...]{return func(args...);}
);
std::future<result_t> ftr = task->get_future();
tasks_.push(std::move(task));
condition_run_.notify_one();
return ftr;
}
// wait for worker threads to finish all tasks without executing join()
void wait() {
std::unique_lock<std::mutex> lck(mutex_);
condition_wait_.wait(lck, [this]{
return tasks_.empty() &&
(waiting_threads_ == static_cast<unsigned int>(threads_.size()));
});
}
private:
void run() {
std::unique_ptr<BasicTask> task = nullptr;
while (true) {
{
std::unique_lock<std::mutex> lck(mutex_);
++waiting_threads_;
condition_wait_.notify_one();
condition_run_.wait(lck, [this] {
return !tasks_.empty() || is_being_destroyed_;
});
--waiting_threads_;
if (tasks_.empty()) return;
task = std::move(tasks_.front());
tasks_.pop();
}
(*task)();
}
}
std::vector<std::thread> threads_;
std::queue<std::unique_ptr<BasicTask>> tasks_;
std::mutex mutex_;
std::condition_variable condition_run_;
std::condition_variable condition_wait_;
bool is_being_destroyed_ = false;
unsigned int waiting_threads_ = 0;
};
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////
} // namespace wtl
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////
#endif /* WTL_CONCURRENT_HPP_ */