forked from Light-City/CPlusPlusThings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
producer_consumer.cpp
100 lines (82 loc) · 1.89 KB
/
producer_consumer.cpp
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
#include <condition_variable>
#include <iostream>
#include <mutex>
#include <thread>
#include <unistd.h>
#include <vector>
using namespace std;
const int MAX_NUM = 10000;
class BoundedBuffer {
public:
BoundedBuffer(size_t n) {
array_.resize(n);
start_pos_ = 0;
end_pos_ = 0;
pos_ = 0;
}
void Produce(int n) {
{
std::unique_lock<std::mutex> lock(mtx_);
// wait for not full
not_full_.wait(lock, [=] { return pos_ < array_.size(); });
usleep(1000 * 400);
array_[end_pos_] = n;
end_pos_ = (end_pos_ + 1) % array_.size();
++pos_;
cout << "Produce pos:" << pos_ << endl;
} // auto unlock
not_empty_.notify_one();
}
int Consume() {
std::unique_lock<std::mutex> lock(mtx_);
// wait for not empty
not_empty_.wait(lock, [=] { return pos_ > 0; });
usleep(1000 * 400);
int n = array_[start_pos_];
start_pos_ = (start_pos_ + 1) % array_.size();
--pos_;
cout << "Consume pos:" << pos_ << endl;
lock.unlock();
not_full_.notify_one();
return n;
}
private:
std::vector<int> array_;
size_t start_pos_;
size_t end_pos_;
size_t pos_;
std::mutex mtx_;
std::condition_variable not_full_;
std::condition_variable not_empty_;
};
BoundedBuffer bb(10);
std::mutex g_mtx;
void Producer() {
int n = 0;
while (n < 100) {
bb.Produce(n);
cout << "Producer:" << n << endl;
n++;
}
bb.Produce(-1);
}
void Consumer() {
std::thread::id thread_id = std::this_thread::get_id();
int n = 0;
do {
n = bb.Consume();
cout << "Consumer thread:" << thread_id << "=====> " << n << endl;
} while (-1 != n);
bb.Produce(-1);
}
int main() {
std::vector<std::thread> t;
t.push_back(std::thread(&Producer));
t.push_back(std::thread(&Consumer));
t.push_back(std::thread(&Consumer));
t.push_back(std::thread(&Consumer));
for (auto &one : t) {
one.join();
}
return 0;
}