-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer_reset.cpp
More file actions
36 lines (29 loc) · 880 Bytes
/
Copy pathtimer_reset.cpp
File metadata and controls
36 lines (29 loc) · 880 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
/*
* Timers may be reset, but only after they have been stopped or timed out and
* the channel is drained.
*
* One major difference between this implementation and the Go implementation
* is how `timer.AfterFunc()` is emulated. In the Go implementation the
* callback occurs in a separate goroutine. In this package the callback
* function is called in the timer thread.
*/
#include <chrono>
#include <iostream>
#include <bongo/time.h>
using namespace std::chrono_literals;
int main() try {
bongo::time::timer t{100ms};
decltype(t)::recv_type v;
if (!t.stop()) {
v << t.c();
}
t.reset(50ms);
v << t.c();
std::cout << "Timed out after "
<< std::chrono::duration_cast<std::chrono::milliseconds>(*v).count()
<< " milliseconds.\n";
return 0;
} catch (std::exception const& e) {
std::cerr << e.what() << "\n";
return 1;
}