-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeadline_context.cpp
More file actions
40 lines (34 loc) · 1 KB
/
Copy pathdeadline_context.cpp
File metadata and controls
40 lines (34 loc) · 1 KB
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
/*
* The function `bongo::context::with_deadline` returns a cancelable context
* that is automatically canceled at the specified point in time.
*/
#include <chrono>
#include <iostream>
#include <optional>
#include <variant>
#include <bongo/context.h>
#include <bongo/time.h>
using namespace std::chrono_literals;
int main() {
auto d = std::chrono::system_clock::now() + 1ms;
auto [ctx, cancel] = bongo::context::with_deadline(bongo::context::background(), d);
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;
}
// Even though ctx will be expired, it is good practice to call its
// cancellation function in any case. Failure to do so may keep the context
// and its parent alive longer than necessary.
cancel();
return 0;
}