Repository navigation
Expand file tree
/
Copy pathqueue.cc
More file actions
229 lines (197 loc) · 3.93 KB
/
Copy pathqueue.cc
File metadata and controls
229 lines (197 loc) · 3.93 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
#include <stack>
#include <deque>
#include <queue>
#include <vector>
#include <mutex>
#include <iostream>
#include <condition_variable>
template <typename T>
class QueueUsingStack
{
public:
void push(const T& val);
void pop();
bool empty();
private:
std::stack<T> spush;
std::stack<T> spop;
};
template <typename T>
void QueueUsingStack<T>::push(const T& val)
{
spush.push(val);
}
template <typename T>
void QueueUsingStack<T>::pop()
{
if (spop.empty()) {
while (!spush.empty()) {
spop.push(spush.top());
spush.pop();
}
}
spop.pop();
}
template <typename T>
bool QueueUsingStack<T>::empty()
{
return (spush.empty() && spop.empty());
}
template <typename T>
class QueueUsingDeque
{
public:
void push(const T& val) { deque.emplace_back(val); }
void pop() { deque.pop_front(); }
T& front() { return deque.front(); }
bool empty() { return (deque.empty()); }
private:
std::deque<T> deque;
};
template <typename T>
class QueueUsingCircularArray
{
public:
QueueUsingCircularArray(int sz) : sz_(sz) { vec_.resize(sz); }
void push(const T& val)
{
if (full_) {
throw "queue full";
}
vec_[write_idx_] = val;
if (++write_idx_ == sz_) {
write_idx_ = 0;
}
if (write_idx_ == read_idx_) {
full_ = true;
}
}
void pop()
{
if (empty()) {
throw "queue empty";
}
if (++read_idx_ == sz_) {
read_idx_ = 0;
}
full_ = false;
}
T& top()
{
if (empty()) {
throw "queue empty";
}
return vec_[read_idx_];
}
bool empty() const { return (!full_ && read_idx_ == write_idx_); }
private:
int sz_;
std::vector<T> vec_;
int read_idx_ = 0;
int write_idx_ = 0;
bool full_ = false;
};
template class QueueUsingCircularArray<int>;
template <typename T>
class ThreadSafeQueue1
{
public:
T wait_pop()
{
std::unique_lock<std::mutex> lock(mutex_);
cond_.wait(lock, [this] { return !queue_.empty(); });
T val = std::move(queue_.front());
queue_.pop();
return val;
}
T pop()
{
std::lock_guard<std::mutex> lock(mutex_);
if (queue_.empty()) {
throw "queue empty";
}
T val = std::move(queue_.front());
queue_.pop();
return val;
}
void push(T val)
{
std::lock_guard<std::mutex> lock(mutex_);
queue_.push(std::move(val));
cond_.notify_one();
}
bool empty() const
{
std::lock_guard<std::mutex> lock(mutex_);
return queue_.empty();
}
private:
std::queue<T> queue_;
mutable std::mutex mutex_;
std::condition_variable cond_;
};
template class ThreadSafeQueue1<int>;
template <typename T>
class ThreadSafeQueue2
{
public:
ThreadSafeQueue2() : head(new Node), tail(head.get()) {}
T wait_pop()
{
std::unique_lock<std::mutex> lock(head_mutex_);
cond_.wait(lock, [this] { return head.get() != getTail(); });
T val = std::move(head->val_);
head = std::move(head->next);
return val;
}
T pop()
{
std::lock_guard<std::mutex> lock(head_mutex_);
if (head.get() == getTail()) {
throw "queue empty";
}
T val = std::move(head->val_);
head = std::move(head->next);
return val;
}
void push(T val)
{
Node* node = new Node;
std::unique_lock<std::mutex> lock(tail_mutex_);
tail->val_ = std::move(val);
tail->next = std::unique_ptr<Node>(node);
tail = tail->next.get();
lock.unlock();
cond_.notify_one();
}
bool empty()
{
std::lock_guard<std::mutex> lock(head_mutex_);
return (head.get() == getTail());
}
private:
struct Node
{
T val_;
std::unique_ptr<Node> next;
};
std::unique_ptr<Node> head;
Node* tail;
std::mutex head_mutex_;
std::mutex tail_mutex_;
std::condition_variable cond_;
Node* getTail()
{
std::lock_guard<std::mutex> tail_lock(tail_mutex_);
return tail;
}
};
template class ThreadSafeQueue2<int>;
int main()
{
ThreadSafeQueue2<int> q;
q.push(1);
q.push(2);
std::cout << q.pop() << "\n";
std::cout << q.pop() << "\n";
}