-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcThreadQueue_V2.h
More file actions
170 lines (142 loc) · 4.24 KB
/
cThreadQueue_V2.h
File metadata and controls
170 lines (142 loc) · 4.24 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
#pragma once
#include <atomic>
#include <cstddef>
#include <mutex>
#include <optional>
#include <queue>
#include <stdexcept>
#include <type_traits>
#include <utility>
template <typename Type>
class cThreadQueue_V2
{
public:
cThreadQueue_V2() noexcept = default;
cThreadQueue_V2(const cThreadQueue_V2 &) = delete;
cThreadQueue_V2 &operator=(const cThreadQueue_V2 &) = delete;
cThreadQueue_V2(cThreadQueue_V2 &&other) noexcept
{
moveFrom(std::move(other));
}
cThreadQueue_V2 &operator=(cThreadQueue_V2 &&other) noexcept
{
if (this != &other)
moveFrom(std::move(other));
return *this;
}
cThreadQueue_V2 &operator=(std::queue<Type> &&queue)
{
std::lock_guard<std::mutex> lock(m_mutex);
m_queue = std::move(queue);
syncCachedSizeLocked();
return *this;
}
~cThreadQueue_V2() = default;
void clear() noexcept
{
std::lock_guard<std::mutex> lock(m_mutex);
m_queue = std::queue<Type>{};
m_queueSize.store(0u, std::memory_order_relaxed);
}
template <typename ValueType = Type>
void push(ValueType &&element)
{
std::lock_guard<std::mutex> lock(m_mutex);
m_queue.push(std::forward<ValueType>(element));
syncCachedSizeLocked();
}
void push(std::queue<Type> elements)
{
if (elements.empty())
return;
std::lock_guard<std::mutex> lock(m_mutex);
while (!elements.empty())
{
m_queue.push(std::move(elements.front()));
elements.pop();
}
syncCachedSizeLocked();
}
template <typename... Args>
void emplace(Args &&...args)
{
std::lock_guard<std::mutex> lock(m_mutex);
m_queue.emplace(std::forward<Args>(args)...);
syncCachedSizeLocked();
}
[[nodiscard]] std::optional<Type> tryGetFront()
{
std::lock_guard<std::mutex> lock(m_mutex);
if (m_queue.empty())
return std::nullopt;
Type element = std::move(m_queue.front());
m_queue.pop();
syncCachedSizeLocked();
return std::optional<Type>(std::move(element));
}
bool tryGetFront(Type &element)
{
auto popped = tryGetFront();
if (!popped.has_value())
return false;
element = std::move(*popped);
return true;
}
[[nodiscard]] Type getFront()
{
auto popped = tryGetFront();
if (!popped.has_value())
throw std::underflow_error("cThreadQueue_V2::getFront called on an empty queue");
return std::move(*popped);
}
[[nodiscard]] std::queue<Type> getFront(std::size_t elementCount)
{
std::queue<Type> elements;
if (elementCount == 0u)
return elements;
std::lock_guard<std::mutex> lock(m_mutex);
while ((elementCount > 0u) && !m_queue.empty())
{
elements.push(std::move(m_queue.front()));
m_queue.pop();
--elementCount;
}
syncCachedSizeLocked();
return elements;
}
[[nodiscard]] std::queue<Type> getAll()
{
std::lock_guard<std::mutex> lock(m_mutex);
std::queue<Type> elements = std::move(m_queue);
m_queue = std::queue<Type>{};
m_queueSize.store(0u, std::memory_order_relaxed);
return elements;
}
[[nodiscard]] bool empty(bool useCached = true) const noexcept
{
return size(useCached) == 0u;
}
[[nodiscard]] std::size_t size(bool useCached = true) const noexcept
{
if (useCached)
return m_queueSize.load(std::memory_order_relaxed);
std::lock_guard<std::mutex> lock(m_mutex);
return m_queue.size();
}
private:
void moveFrom(cThreadQueue_V2 &&other) noexcept
{
std::scoped_lock lock(m_mutex, other.m_mutex);
m_queue = std::move(other.m_queue);
syncCachedSizeLocked();
other.m_queue = std::queue<Type>{};
other.m_queueSize.store(0u, std::memory_order_relaxed);
}
void syncCachedSizeLocked() noexcept
{
m_queueSize.store(m_queue.size(), std::memory_order_relaxed);
}
mutable std::mutex m_mutex;
std::queue<Type> m_queue;
std::atomic<std::size_t> m_queueSize{0u};
};