This commit is contained in:
aozhiwei 2023-05-27 14:02:25 +08:00
parent e1e32e2800
commit c85563a289
3 changed files with 11 additions and 4 deletions

View File

@ -11,20 +11,21 @@ void CoMgr::Init()
(
[] (Coroutine* co)
{
a8::XPrintf("call 1\n", {});
a8::XPrintf("call 1 %d\n", {a8::XGetTickCount()});
for (int i = 0; i < 10; ++i) {
co->CoYield();
a8::XPrintf("call A %d\n", {i});
co->CoAwait(co->Sleep(1000));
a8::XPrintf("call A %d %d\n", {i, a8::XGetTickCount()});
}
});
CreateCo
(
[] (Coroutine* co)
{
a8::XPrintf("call 10\n", {});
a8::XPrintf("call 10 %d\n", {a8::XGetTickCount()});
for (int i = 0; i < 10; ++i) {
co->CoYield();
a8::XPrintf("call B %d\n", {i});
a8::XPrintf("call B %d %d\n", {i, a8::XGetTickCount()});
}
});
}

View File

@ -14,6 +14,11 @@ void Awaiter::Await(std::shared_ptr<Awaiter> notifyer)
DoAwait();
}
std::shared_ptr<Awaiter> Awaiter::Sleep(int time)
{
return std::make_shared<TimerPromise>(time);
}
void Promise::DoAwait()
{

View File

@ -33,6 +33,7 @@ class Awaiter : public std::enable_shared_from_this<Awaiter>
}
virtual void DoResume() {};
std::shared_ptr<Awaiter> Sleep(int time);
protected:
std::list<std::weak_ptr<Awaiter>> notifyers_;