-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathqueue.hpp
More file actions
289 lines (240 loc) · 7.08 KB
/
queue.hpp
File metadata and controls
289 lines (240 loc) · 7.08 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#pragma once
#include <utils/object.hpp>
#include <chrono>
#include <condition_variable>
#include <mutex>
#include <optional>
#include <utility>
#include <queue>
template<typename T>
class Queue : noncopyable
{
std::queue<T> m_queue;
std::atomic<bool> m_stop = false; // 改为 atomic
mutable std::mutex m_mutex;
std::condition_variable m_condEmpty;
std::condition_variable m_condFull;
std::size_t m_maxSize = 0;
public:
Queue() = default;
explicit Queue(std::size_t maxSize)
: m_maxSize(maxSize == 0 ? 1 : maxSize)
{}
~Queue()
{
stop();
clear();
}
// 阻塞push,直到有空间可用或队列停止
[[nodiscard]] auto push(T &&item) -> bool
{
bool success = false;
{
std::unique_lock lock(m_mutex);
m_condFull.wait(lock, [this]() {
return m_stop.load() || m_maxSize == 0 || m_queue.size() < m_maxSize;
});
if (m_stop.load()) {
return false;
}
m_queue.push(std::move(item));
success = true;
}
if (success) {
m_condEmpty.notify_one();
}
return success;
}
// 阻塞push,复制版本
[[nodiscard]] auto push(const T &item) -> bool
{
T temp = item;
return push(std::move(temp));
}
// 非阻塞push,立即返回结果
[[nodiscard]] auto try_push(T &&item) -> bool
{
std::unique_lock lock(m_mutex, std::try_to_lock);
if (!lock.owns_lock() || m_stop.load()) {
return false;
}
if (m_maxSize > 0 && m_queue.size() >= m_maxSize) {
return false;
}
m_queue.push(std::move(item));
lock.unlock();
m_condEmpty.notify_one();
return true;
}
// 带超时的push
template<typename Rep, typename Period>
[[nodiscard]] auto push_for(T &&item, const std::chrono::duration<Rep, Period> &timeout) -> bool
{
bool success = false;
{
std::unique_lock lock(m_mutex);
if (!m_condFull.wait_for(lock, timeout, [this]() {
return m_stop.load() || m_maxSize == 0 || m_queue.size() < m_maxSize;
})) {
return false; // 超时
}
if (m_stop.load()) {
return false;
}
m_queue.push(std::move(item));
success = true;
}
if (success) {
m_condEmpty.notify_one();
}
return success;
}
// 阻塞pop,使用输出参数
[[nodiscard]] auto pop(T &item) -> bool
{
std::unique_lock lock(m_mutex);
m_condEmpty.wait(lock, [this]() { return !m_queue.empty() || m_stop.load(); });
if (m_queue.empty() || m_stop.load()) {
return false;
}
item = std::move(m_queue.front());
m_queue.pop();
lock.unlock();
m_condFull.notify_one();
return true;
}
// 阻塞pop,返回optional(推荐使用)
[[nodiscard]] auto pop() -> std::optional<T>
{
std::unique_lock lock(m_mutex);
m_condEmpty.wait(lock, [this]() { return !m_queue.empty() || m_stop.load(); });
if (m_queue.empty() || m_stop.load()) {
return std::nullopt;
}
T item = std::move(m_queue.front());
m_queue.pop();
lock.unlock();
m_condFull.notify_one();
return std::move(item);
}
// 非阻塞pop,使用输出参数
[[nodiscard]] auto try_pop(T &item) -> bool
{
std::unique_lock lock(m_mutex, std::try_to_lock);
if (!lock.owns_lock() || m_queue.empty() || m_stop.load()) {
return false;
}
item = std::move(m_queue.front());
m_queue.pop();
lock.unlock();
m_condFull.notify_one();
return true;
}
// 非阻塞pop,返回optional
[[nodiscard]] auto try_pop() -> std::optional<T>
{
std::unique_lock lock(m_mutex, std::try_to_lock);
if (!lock.owns_lock() || m_queue.empty() || m_stop.load()) {
return std::nullopt;
}
T item = std::move(m_queue.front());
m_queue.pop();
lock.unlock();
m_condFull.notify_one();
return std::move(item);
}
// 带超时的pop,使用输出参数
template<typename Rep, typename Period>
[[nodiscard]] auto pop_for(T &item, const std::chrono::duration<Rep, Period> &timeout) -> bool
{
std::unique_lock lock(m_mutex);
if (!m_condEmpty.wait_for(lock, timeout, [this]() {
return !m_queue.empty() || m_stop.load();
})) {
return false; // 超时
}
if (m_queue.empty() || m_stop.load()) {
return false;
}
item = std::move(m_queue.front());
m_queue.pop();
lock.unlock();
m_condFull.notify_one();
return true;
}
// 带超时的pop,返回optional
template<typename Rep, typename Period>
[[nodiscard]] auto pop_for(const std::chrono::duration<Rep, Period> &timeout)
-> std::optional<T>
{
std::unique_lock lock(m_mutex);
if (!m_condEmpty.wait_for(lock, timeout, [this]() {
return !m_queue.empty() || m_stop.load();
})) {
return std::nullopt; // 超时
}
if (m_queue.empty() || m_stop.load()) {
return std::nullopt;
}
T item = std::move(m_queue.front());
m_queue.pop();
lock.unlock();
m_condFull.notify_one();
return std::move(item);
}
// 设置最大容量
void setMaxSize(std::size_t maxSize)
{
{
std::scoped_lock guard(m_mutex);
m_maxSize = maxSize == 0 ? 1 : maxSize;
}
m_condFull.notify_all();
}
[[nodiscard]] auto getMaxSize() const -> std::size_t
{
std::scoped_lock guard(m_mutex);
return m_maxSize;
}
[[nodiscard]] auto size() const -> std::size_t
{
std::scoped_lock guard(m_mutex);
return m_queue.size();
}
[[nodiscard]] auto empty() const -> bool
{
std::scoped_lock guard(m_mutex);
return m_queue.empty();
}
// 清空队列
void clear()
{
std::scoped_lock guard(m_mutex);
std::queue<T>().swap(m_queue);
m_condFull.notify_all();
}
// 清空并返回所有剩余元素
[[nodiscard]] auto flush() -> std::vector<T>
{
std::vector<T> result;
{
std::scoped_lock guard(m_mutex);
while (!m_queue.empty()) {
result.push_back(std::move(m_queue.front()));
m_queue.pop();
}
}
m_condFull.notify_all();
return result;
}
[[nodiscard]] auto isStopped() const -> bool { return m_stop.load(); }
void start() { m_stop.store(false); }
void stop()
{
bool expected = false;
if (m_stop.compare_exchange_strong(expected, true)) {
m_condEmpty.notify_all();
m_condFull.notify_all();
}
}
};