-
Notifications
You must be signed in to change notification settings - Fork 0
/
safe-queue.hpp
58 lines (49 loc) · 1.17 KB
/
safe-queue.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
#ifndef BOMBERMAN_SAFE_QUEUE_HPP
#define BOMBERMAN_SAFE_QUEUE_HPP
#include <deque>
#include <thread>
template <typename T>
class safe_queue {
const size_t max_size;
std::deque<T> que;
bool destroying = false;
std::condition_variable cond_que;
std::mutex mutex_que;
public:
safe_queue(size_t max_size) : max_size(max_size) {}
void interrupt() {
std::scoped_lock lock {mutex_que};
destroying = true;
cond_que.notify_all();
}
void push(T t) {
std::scoped_lock lock {mutex_que};
if (que.size() == max_size) {
throw std::runtime_error("Queue full");
}
que.push_back(t);
cond_que.notify_one();
}
T pop() {
std::unique_lock lock {mutex_que};
if (que.empty()) {
cond_que.wait(lock,
[this] { return !que.empty() || destroying; }
);
}
if (destroying) {
throw std::runtime_error("Queue destroyed");
}
T val = que.front();
que.pop_front();
return val;
}
T try_pop() {
std::unique_lock lock {mutex_que};
if (que.empty()) { throw std::runtime_error("Queue empty"); }
T val = que.front();
que.pop_front();
return val;
}
};
#endif //BOMBERMAN_SAFE_QUEUE_HPP