-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathutil_blocking_queue.cpp
More file actions
132 lines (107 loc) · 2.51 KB
/
util_blocking_queue.cpp
File metadata and controls
132 lines (107 loc) · 2.51 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
/*!
* \brief Blocking queue.
* Mainly implemented by thread, queue and condition_variable.
*/
#include <iostream>
#include <thread>
#include <queue>
#include <condition_variable>
template<typename T>
class BlockingQueue {
public:
BlockingQueue(int capacity) {
capacity_ = capacity;
};
~BlockingQueue() {};
int size() const;
void push(const T& t);
void wait_and_push(const T& t);
bool try_pop(T* t);
void wait_and_pop(T* t);
private:
int capacity_;
std::queue<T> queue_;
mutable std::mutex mutex_;
std::condition_variable pop_cond_var_;
std::condition_variable push_cond_var_;
};
template<typename T>
int BlockingQueue<T>::size() const {
std::unique_lock <std::mutex> lock(mutex_);
return queue_.size();
}
template<typename T>
void BlockingQueue<T>::push(const T& t) {
std::unique_lock <std::mutex> lock(mutex_);
queue_.push(t);
lock.unlock();
pop_cond_var_.notify_one();
}
template<typename T>
void BlockingQueue<T>::wait_and_push(const T& t) {
std::unique_lock <std::mutex> lock(mutex_);
while(queue_.size() >= capacity_)
push_cond_var_.wait(lock);
queue_.push(t);
pop_cond_var_.notify_one();
}
template<typename T>
bool BlockingQueue<T>::try_pop(T* t) {
std::unique_lock <std::mutex> lock(mutex_);
if (queue_.empty())
return false;
*t = queue_.front();
queue_.pop();
push_cond_var_.notify_one();
return true;
}
template<typename T>
void BlockingQueue<T>::wait_and_pop(T* t) {
std::unique_lock <std::mutex> lock(mutex_);
while (queue_.empty())
pop_cond_var_.wait(lock);
*t = queue_.front();
queue_.pop();
push_cond_var_.notify_one();
}
///////////////////////////////////////////////////////////
template<typename T>
class Worker {
public:
Worker(int capacity) {
blocking_queue_ = new BlockingQueue<T>(capacity);
num_ = 0;
}
~Worker() {
delete blocking_queue_;
}
void Produce() {
while (1) {
blocking_queue_->wait_and_push(num_++);
}
}
void Consume() {
while (1) {
T data;
blocking_queue_->wait_and_pop(&data);
std::cout << "(" << data << ", " << blocking_queue_->size() << ")";
}
}
private:
BlockingQueue<T> *blocking_queue_;
int num_;
};
int main() {
auto Func_Produce = [](Worker<int> *worker) {
worker->Produce();
};
auto Func_Consume = [](Worker<int> *worker) {
worker->Consume();
};
Worker<int> worker(2000);
std::thread t0 = std::thread(Func_Produce, &worker);
std::thread t1 = std::thread(Func_Consume, &worker);
t0.join();
t1.join();
return 0;
}