-
Notifications
You must be signed in to change notification settings - Fork 0
/
test1.cpp
167 lines (134 loc) · 3.94 KB
/
test1.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
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
#include <iostream>
#include <stdint.h>
#include <boost/circular_buffer.hpp>
#include <condition_variable>
#include <thread>
// individual request
struct upload_page_t {
size_t buffer_size;
uint8_t *buffer;
};
// ring buffer with protection for overwrites
template <typename T>
class ring_buffer {
public:
ring_buffer(size_t size) {
cb.set_capacity(size);
}
~ring_buffer() {
}
void read(T& entry) {
{
std::unique_lock<std::mutex> lk(cv_mutex);
//std::cout << "Read wait" << std::endl;
cv.wait(lk, [this] {
std::cout << "Read woke up, test=" << (cb.size() > 0) << std::endl;
return 0 < cb.size();});
auto iter = cb.begin();
entry = *iter;
cb.pop_front();
std::cout << "Read: size=" << cb.size() << std::endl;
//std::cout << "Read: " << entry << " [" << cb.size() << ", " << cb.capacity() << "]" << std::endl;
std::cout << "Read notify_one" << std::endl;
}
cv.notify_one();
}
void read2(T& entry) {
{
std::unique_lock<std::mutex> lk(cv_mutex);
//std::cout << "Read wait" << std::endl;
cv.wait(lk, [this] {
std::cout << "Read2 woke up, test=" << false << std::endl;
return false;});
auto iter = cb.begin();
entry = *iter;
cb.pop_front();
std::cout << "Read2: size=" << cb.size() << std::endl;
//std::cout << "Read: " << entry << " [" << cb.size() << ", " << cb.capacity() << "]" << std::endl;
std::cout << "Read2 notify_one" << std::endl;
}
cv.notify_one();
}
void write(const T& entry) {
{
std::unique_lock<std::mutex> lk(cv_mutex);
//std::cout << "Write wait" << std::endl;
cv.wait(lk, [this] {
std::cout << "Write woke up, test=" << (cb.size() < cb.capacity()) << std::endl;
return cb.size() < cb.capacity();});
cb.push_back(entry);
std::cout << "Write: size=" << cb.size() << std::endl;
//std::cout << "Write: " << entry << " [" << cb.size() << ", " << cb.capacity() << "]" << std::endl;
std::cout << "Write notify_one" << std::endl;
}
cv.notify_one();
}
size_t get_number_entries() {
std::unique_lock<std::mutex> lk(cv_mutex);
return cb.size();
}
private:
boost::circular_buffer<T> cb;
std::condition_variable cv;
std::mutex cv_mutex;
};
std::mutex start_mtx;
std::condition_variable start_cv;
bool ready = false;
void write_loop(ring_buffer<int> *buffer) {
// sync start
{
std::unique_lock<std::mutex> lck(start_mtx);
while (!ready) {
start_cv.wait(lck);
}
}
for (int i = 0; i < 50000; ++i) {
buffer->write(i);
//sleep(1);
}
}
void read_loop(ring_buffer<int> *buffer) {
// sync start
{
std::unique_lock<std::mutex> lck(start_mtx);
while (!ready) {
start_cv.wait(lck);
}
}
for (int i = 0; i < 50000; ++i) {
int val;
buffer->read(val);
}
}
void read_loop2(ring_buffer<int> *buffer) {
// sync start
{
std::unique_lock<std::mutex> lck(start_mtx);
while (!ready) {
start_cv.wait(lck);
}
}
for (int i = 0; i < 50000; ++i) {
int val;
buffer->read2(val);
}
}
void go() {
std::unique_lock<std::mutex> lck(start_mtx);
ready = true;
start_cv.notify_all();
}
int main() {
ring_buffer<int> buffer(1000);
std::thread writer(write_loop, &buffer);
std::thread writer2(write_loop, &buffer);
std::thread reader(read_loop, &buffer);
std::thread reader2(read_loop2, &buffer);
go();
writer.join();
writer2.join();
reader.join();
reader2.join();
return 0;
}