-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathchannel_read_write_nolock.cpp
55 lines (46 loc) · 1.3 KB
/
channel_read_write_nolock.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
/**
* @author github.com/luncliff (luncliff@gmail.com)
*/
#undef NDEBUG
#include <cassert>
#include <coroutine/channel.hpp>
#include <coroutine/return.h>
using namespace std;
using namespace coro;
using channel_without_lock_t = channel<int>;
#if defined(__GNUC__)
using no_return_t = coro::null_frame_t;
#else
using no_return_t = std::nullptr_t;
#endif
auto write_to(channel_without_lock_t& ch, int value, bool ok = false)
-> no_return_t {
ok = co_await ch.write(value);
if (ok == false)
// !!!!!
// seems like clang optimizer is removing `value`.
// so using it in some pass makes
// the symbol and its memory location alive
// !!!!!
value += 1;
assert(ok);
}
auto read_from(channel_without_lock_t& ch, int& ref, bool ok = false)
-> no_return_t {
tie(ref, ok) = co_await ch.read();
assert(ok);
}
int main(int, char*[]) {
const auto list = {1, 2, 3};
channel_without_lock_t ch{};
int storage = 0;
for (auto i : list) {
read_from(ch, storage); // Reader coroutine will suspend
assert(storage != i); // so no read occurs
}
for (auto i : list) {
write_to(ch, i); // writer will send a value
assert(storage == i); // stored value is same with sent value
}
return EXIT_SUCCESS;
}