-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathThreadsafeQueue.h
More file actions
128 lines (109 loc) · 3.39 KB
/
ThreadsafeQueue.h
File metadata and controls
128 lines (109 loc) · 3.39 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
// ===========================================================================
// ThreadsafeQueue.h
// ===========================================================================
#pragma once
#include <condition_variable>
#include <mutex>
#include <optional>
#include <queue>
namespace Concurrency_ThreadsafeQueue
{
template<typename T>
class ThreadsafeQueue
{
private:
std::queue<T> m_data;
mutable std::mutex m_mutex;
std::condition_variable m_condition;
public:
ThreadsafeQueue() {}
// copy and move constructor may be useful
ThreadsafeQueue(const ThreadsafeQueue& other)
{
std::lock_guard<std::mutex> guard{ other.m_mutex };
m_data = other.m_data;
}
ThreadsafeQueue(ThreadsafeQueue&& other) noexcept
{
std::lock_guard<std::mutex> guard{ other.m_mutex };
m_data = std::move(other.m_data);
}
ThreadsafeQueue& operator= (const ThreadsafeQueue& other)
{
if (&other == this)
return *this;
std::scoped_lock<std::mutex> guard{ m_mutex, other.m_mutex };
m_data = other.m_data;
return *this;
}
ThreadsafeQueue& operator= (ThreadsafeQueue&& other) noexcept
{
if (&other == this)
return *this;
std::scoped_lock<std::mutex> guard{ m_mutex, other.m_mutex };
m_data = std::move (other.m_data);
return *this;
}
void push(const T& value)
{
std::unique_lock<std::mutex> guard{ m_mutex };
m_data.push(value);
guard.unlock();
m_condition.notify_one();
}
void push(T&& value)
{
std::unique_lock<std::mutex> guard{ m_mutex };
m_data.push(std::move(value));
guard.unlock();
m_condition.notify_one();
}
bool tryPop(T& value)
{
std::lock_guard<std::mutex> guard{ m_mutex };
if (m_data.empty()) {
return false;
}
else {
value = std::move(m_data.front());
m_data.pop();
return true;
}
}
std::optional<T> tryPop()
{
std::lock_guard<std::mutex> guard{ m_mutex };
if (m_data.empty()) {
return std::optional<T>(std::nullopt);
}
else {
std::optional<T> result{ std::move(m_data.front()) };
m_data.pop();
return result;
}
}
void waitAndPop(T& value)
{
std::unique_lock<std::mutex> guard{ m_mutex };
m_condition.wait(guard, [this]() {
return !m_data.empty();
}
);
value = std::move(m_data.front());
m_data.pop();
}
bool empty() const
{
std::lock_guard<std::mutex> guard{ m_mutex };
return m_data.empty();
}
size_t size() const
{
std::lock_guard<std::mutex> guard{ m_mutex };
return m_data.size();
}
};
}
// ===========================================================================
// End-of-File
// ===========================================================================