#include <chrono>
#include <iostream>
#include <thread>
template <mp_coro::specialization_of<std::chrono::duration> D>
struct sleep_for {
D duration;
bool await_ready() const noexcept { return duration <= D::zero(); }
std::coroutine_handle<> await_suspend(std::coroutine_handle<> coro) const {
std::this_thread::sleep_for(duration);
return coro;
}
static void await_resume() noexcept {}
};
using namespace std::chrono_literals;
std::cout << "sleepy(): about to sleep\n";
co_await sleep_for {1s};
std::cout << "sleepy(): about to return\n";
}
int main() {
try {
} catch (const std::exception &ex) {
std::cout << "Unhandled exception: " << ex.what() << '\n';
}
}
Task that produces a value of type T: to get that value, simply await the task.
decltype(auto) sync_await(A &&awaitable)
Creates a synchronized task from the awaitable, starts it, and waits for it to complete,...