-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathtst-basic-timer.cpp
More file actions
38 lines (31 loc) · 1.83 KB
/
tst-basic-timer.cpp
File metadata and controls
38 lines (31 loc) · 1.83 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
// examples/tst-basic-timer.cpp -*-C++-*-
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// ----------------------------------------------------------------------------
#include <beman/execution/execution.hpp>
#include <iostream>
#include "tst.hpp"
namespace ex = beman::execution;
// ----------------------------------------------------------------------------
struct receiver {
using receiver_concept = ex::receiver_tag;
auto set_value() && noexcept { std::cout << "timer done\n"; }
auto set_error(const std::exception_ptr&) && noexcept { std::cout << "timer error\n"; }
auto set_stopped() && noexcept { std::cout << "timer stopped\n"; }
};
std::ostream& fmt_now(std::ostream& os) { return os << std::chrono::system_clock::now(); }
auto main() -> int {
std::size_t count{3};
ex::sync_wait(tst::repeat_effect_until(ex::just() | ex::then([]() noexcept { std::cout << "effect\n"; }),
[&count]() noexcept { return --count == 0u; }));
tst::timer timer{};
std::cout << fmt_now << ": start\n";
ex::sync_wait(
ex::when_all(tst::resume_after(timer.get_token(), std::chrono::milliseconds(1000)) |
ex::then([]() noexcept { std::cout << fmt_now << ": 1000ms timer fired\n"; }),
tst::resume_after(timer.get_token(), std::chrono::milliseconds(500)) |
ex::then([]() noexcept { std::cout << fmt_now << ": 500ms timer fired\n"; }),
tst::resume_after(timer.get_token(), std::chrono::milliseconds(1500)) |
ex::then([]() noexcept { std::cout << fmt_now << ": 1500ms timer fired\n"; }),
timer.when_done() | ex::then([]() noexcept { std::cout << fmt_now << ": timer done\n"; }),
ex::just()));
}