forked from luncliff/coroutine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel_close_write.cpp
36 lines (28 loc) · 888 Bytes
/
channel_close_write.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
/**
* @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>;
int main(int, char*[]) {
auto ch = make_unique<channel_without_lock_t>();
bool ok = true;
auto coro_write = [&ok](auto& ch, auto value) -> frame_t {
ok = co_await ch.write(value);
};
// coroutine will suspend and wait in the channel
auto h = coro_write(*ch, int{});
{
auto truncator = move(ch); // if channel is destroyed ...
}
assert(ch.get() == nullptr);
coroutine_handle<void>& coro = h;
assert(coro.done()); // coroutine is in done state
coro.destroy(); // destroy to prevent leak
assert(ok == false); // and channel returned false
return EXIT_SUCCESS;
}