#include <chrono>
#include <iostream>
#include <thread>
struct awaitable {
int result;
static bool await_ready() noexcept {
return false;
}
void await_suspend(std::coroutine_handle<> handle) {
auto work = [&, handle] {
using namespace std::chrono_literals;
std::this_thread::sleep_for(200ms);
result = 42;
handle.resume();
};
std::jthread(work).detach();
}
int await_resume() noexcept {
return result;
}
};
co_return co_await awaitable {};
}
const auto task = async_foo();
const long i = co_await task;
co_return i + 23;
}
int main() {
try {
} catch (const std::exception &ex) {
std::cout << "Unhandled exception caught: " << 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,...