#include<coroutine>#include<iostream>#include<thread>namespace Coroutine {structtask{structpromise_type{promise_type(){
std::cout <<"1.create promie object\n";}
task get_return_object(){
std::cout <<"2.create coroutine return object, and the coroutine is created now\n";return{std::coroutine_handle<task::promise_type>::from_promise(*this)};}
std::suspend_never initial_suspend(){
std::cout <<"3.do you want to susupend the current coroutine?\n";
std::cout <<"4.don't suspend because return std::suspend_never, so continue to execute coroutine body\n";return{};}
std::suspend_never final_suspend()noexcept{
std::cout <<"13.coroutine body finished, do you want to susupend the current coroutine?\n";
std::cout <<"14.don't suspend because return std::suspend_never, and the continue will be automatically destroyed, bye\n";return{};}voidreturn_void(){
std::cout <<"12.coroutine don't return value, so return_void is called\n";}voidunhandled_exception(){}};
std::coroutine_handle<task::promise_type> handle_;};structawaiter{boolawait_ready(){
std::cout <<"6.do you want to suspend current coroutine?\n";
std::cout <<"7.yes, suspend becase awaiter.await_ready() return false\n";returnfalse;}voidawait_suspend(
std::coroutine_handle<task::promise_type> handle){
std::cout <<"8.execute awaiter.await_suspend()\n";
std::thread([handle]()mutable{handle();}).detach();
std::cout <<"9.a new thread lauched, and will return back to caller\n";}voidawait_resume(){}};
task test(){
std::cout <<"5.begin to execute coroutine body, the thread id="<< std::this_thread::get_id()<<"\n";//#1co_await awaiter{};
std::cout <<"11.coroutine resumed, continue execcute coroutine body now, the thread id="<< std::this_thread::get_id()<<"\n";//#3}}// namespace Coroutineintmain(){Coroutine::test();
std::cout <<"10.come back to caller becuase of co_await awaiter\n";
std::this_thread::sleep_for(std::chrono::seconds(1));return0;}
测试输出:
1.create promie object
2.create coroutine return object,and the coroutine is created now
3.do you want to susupend the current coroutine?4.don't suspend because return std::suspend_never, so continue to execute coroutine body
5.begin to execute coroutine body, the thread id=0x10e1c1dc06.do you want to suspend current coroutine?7.yes, suspend becase awaiter.await_ready()returnfalse8.execute awaiter.await_suspend()9.a new thread lauched,and will return back to caller
10.come back to caller becuase of co_await awaiter
11.coroutine resumed,continue execcute coroutine body now, the thread id=0x700001dc700012.coroutine don't return value, so return_void is called
13.coroutine body finished,do you want to susupend the current coroutine?14.don't suspend because return std::suspend_never,and the continue will be automatically destroyed, bye