-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
41 lines (32 loc) · 734 Bytes
/
test.cpp
File metadata and controls
41 lines (32 loc) · 734 Bytes
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
#include <iostream>
#include <memory>
#include <vector>
#include <thread>
#include "buffering.hpp"
void reader(decltype(TBuffer<int, 3>::make()) tb) {
while (true) {
auto p = tb->acquire();
std::cout << *p << std::endl;
}
}
void writer(decltype(TBuffer<int, 3>::make()) tb) {
for (int i = 0; i < 100000;) {
auto p = tb->select();
if (p != nullptr) {
*p = i;
++i;
}
}
}
int main() {
auto tb = TBuffer<int, 3>::make();
auto read_th1 = std::thread(reader, tb);
auto read_th2 = std::thread(reader, tb);
auto write_th1 = std::thread(writer, tb);
auto write_th2 = std::thread(writer, tb);
read_th1.join();
read_th2.join();
write_th1.join();
write_th2.join();
return 0;
}