-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
37 lines (33 loc) · 850 Bytes
/
main.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
#include <iostream>
#include <thread>
#include "spsc_queue.hpp"
int main () {
spsc_queue<int> q{8};
std::atomic_bool done{false};
static constexpr auto max = 500;
std::thread producer{[&] () {
for (auto ctr = 0; ctr < max; ++ctr) {
while (!q.push (ctr)) {
std::this_thread::yield ();
}
}
}};
std::thread consumer{[&] () {
auto expected = 0;
bool running = true;
while (running) {
if (std::optional<int> const v = q.pop ()) {
assert (v == expected);
++expected;
} else {
running = !done.load (std::memory_order_relaxed);
std::this_thread::yield ();
}
}
assert (expected == max);
}};
producer.join ();
// Tell the consumer to stop once it has drained the queue.
done.store (true, std::memory_order_relaxed);
consumer.join ();
}