/
example.cc
80 lines (71 loc) · 2.03 KB
/
example.cc
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
#include <trio.h>
#include <atomic>
#include <iostream>
#include <string>
#include <thread>
#include <vector>
using namespace std::literals::chrono_literals;
bs::Trio<std::vector<std::string>> buf;
std::vector<std::string> consumer_strings;
std::vector<std::string> producer_strings;
std::atomic<bool> stop{false};
namespace {
const size_t k_num_writes = 10000000;
}
void consumer() {
while (!stop) {
const auto& [strings, changed] = buf.Read();
for (auto& str : strings) {
if (changed) {
consumer_strings.emplace_back(std::move(str));
}
}
strings.clear();
std::this_thread::yield();
}
}
void producer() {
while (!stop) {
auto& v = buf.Write();
v.clear();
int sz = rand() % 1000;
for (int i = 0; i < sz; ++i) {
int len = rand() % 40;
std::string s;
for (int j = 0; j < len; ++j) {
s += 'a' + (rand() % 26);
}
producer_strings.push_back(s);
v.emplace_back(std::move(s));
if (producer_strings.size() >= k_num_writes) {
stop = true;
break;
}
}
buf.Commit();
std::this_thread::yield();
}
}
int main() {
std::thread consumer_thread(consumer);
std::thread producer_thread(producer);
while (!stop) {
std::this_thread::sleep_for(10ms);
}
consumer_thread.join();
producer_thread.join();
int cons = 0, prod = 0;
while (cons < consumer_strings.size() && prod < producer_strings.size()) {
if (consumer_strings[cons] != producer_strings[prod]) {
++prod;
continue;
}
++cons;
}
if (cons < consumer_strings.size()) {
std::cout << "Data mismatch: cannot find string '" << consumer_strings[cons] << "'" << std::endl;
return 1;
}
std::cout << "Success. produced: " << producer_strings.size() << "; consumed: " << consumer_strings.size() << std::endl;
return 0;
}