-
Notifications
You must be signed in to change notification settings - Fork 1
/
await.cpp
30 lines (27 loc) · 924 Bytes
/
await.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
#include <chrono>
#include <unifex/sync_wait.hpp>
#include <unifex/task.hpp>
#include <unifex/timed_single_thread_context.hpp>
#include <unifex/when_all.hpp>
template <typename Scheduler>
requires unifex::scheduler<Scheduler>
auto sleep(Scheduler scheduler, int n) -> unifex::task<void> {
using namespace std::chrono_literals;
for (size_t i = 0; i < n; i += 1) {
co_await unifex::schedule_after(scheduler, 300ms);
}
}
template <typename Scheduler>
requires unifex::scheduler<Scheduler>
auto await_sleep(Scheduler scheduler) -> unifex::task<void> {
auto task1 = sleep(scheduler, 1);
co_await std::move(task1);
co_await unifex::when_all(sleep(scheduler, 2), sleep(scheduler, 1));
}
auto main() -> int {
unifex::timed_single_thread_context context;
auto scheduler = context.get_scheduler();
// auto task = sleep(scheduler);
auto task = await_sleep(scheduler);
unifex::sync_wait(std::move(task));
}