-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeout_context.cpp
More file actions
41 lines (35 loc) · 988 Bytes
/
Copy pathtimeout_context.cpp
File metadata and controls
41 lines (35 loc) · 988 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
/*
* The function `bongo::context::with_timeout` returns a cancelable context
* that is automatically canceled after the specified duration.
*/
#include <chrono>
#include <iostream>
#include <optional>
#include <variant>
#include <bongo/bongo.h>
#include <bongo/context.h>
using namespace std::chrono_literals;
int main() try {
// Pass a context with a timeout to tell a blocking function that should
// abandon its work after the timeout elapses.
auto [ctx, cancel] = bongo::context::with_timeout(bongo::context::background(), 1ms);
auto t = bongo::time::timer{1s};
decltype(t)::recv_type v0;
std::optional<std::monostate> v1;
switch (bongo::select(
bongo::recv_select_case(t.c(), v0),
bongo::recv_select_case(ctx->done(), v1)
)) {
case 0:
std::cout << "overslept\n";
break;
case 1:
std::cerr << ctx->err() << "\n";
break;
}
cancel();
return 0;
} catch (std::exception const& e) {
std::cerr << e.what() << "\n";
return 1;
}