-
Notifications
You must be signed in to change notification settings - Fork 0
/
blocking_queue.h
94 lines (80 loc) · 2.04 KB
/
blocking_queue.h
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
#ifndef BLOCKING_QUEUE_
#define BLOCKING_QUEUE_
#include <boost/thread/condition_variable.hpp>
#include <boost/thread/mutex.hpp>
#include <queue>
#include <optional>
template <typename T>
class BlockingQueue {
public:
explicit BlockingQueue(size_t max_length) : max_length_(max_length), is_locked_(false) {}
void replace(std::queue<T> queue) {
boost::mutex::scoped_lock lock(mutex_);
queue_ = std::move(queue);
condition_.notify_one();
}
void clear() {
boost::mutex::scoped_lock lock(mutex_);
std::queue<T> empty;
std::swap(queue_, empty);
condition_.notify_one();
}
void push(const T& value) {
boost::mutex::scoped_lock lock(mutex_);
while (queue_.size() >= max_length_ || is_locked_) {
condition_full_.wait(lock);
}
queue_.push(value);
condition_.notify_one();
}
std::optional<T> popOrEmpty() {
boost::mutex::scoped_lock lock(mutex_);
if (queue_.empty() || is_locked_) {
return {};
}
T value = std::move(queue_.front());
queue_.pop();
if (queue_.size() < max_length_) {
condition_full_.notify_one();
}
return value;
}
T pop() {
boost::mutex::scoped_lock lock(mutex_);
while (queue_.empty() || is_locked_) {
condition_.wait(lock);
}
T value = std::move(queue_.front());
queue_.pop();
if (queue_.size() < max_length_) {
condition_full_.notify_one();
}
return value;
}
bool empty() const {
boost::mutex::scoped_lock lock(mutex_);
return queue_.empty();
}
size_t size() const {
boost::mutex::scoped_lock lock(mutex_);
return queue_.size();
}
void lock() {
boost::mutex::scoped_lock lock(mutex_);
is_locked_ = true;
}
void unlock() {
boost::mutex::scoped_lock lock(mutex_);
is_locked_ = false;
condition_.notify_all();
condition_full_.notify_all();
}
private:
mutable boost::mutex mutex_;
boost::condition_variable condition_;
boost::condition_variable condition_full_;
std::queue<T> queue_;
size_t max_length_;
bool is_locked_;
};
#endif