c++ - Can I call coroutine_handle::resume during await_suspend? -


consider following code:

coroutine_handle<> g_handle; atomic<int> g_ready;  void worker_thread() {   if (++g_ready == 2) g_handle.resume(); }  struct awaitable {   bool await_ready() const { return false; }   bool await_suspend(coroutine_handle<> h) {     g_handle = h;     if (++g_ready == 2) return false;     // worker_thread can call h.resume() @ point     return true;   }   void await_resume() {} };  future coroutine() {   awaitable a;   std::thread(worker_thread).detach();   co_await a; // compiles as:               // if (a.await_suspend(h)) {               //   // worker_thread can call h.resume() @ point               //   return;               // } } 

here worker_thread can call h.resume(); when coroutine still executing await_suspend or between await_suspend() , return in coroutine.

the coroutines ts says resume can called when coroutine suspended.

is considered suspended during execution of await_suspend?

yes, text of linked coroutines ts states, 5.3.8 paragraph 3-5. emphasis mine:

5 await-expression evaluates await-ready expression, then: —

(5.1) if result false , coroutine considered suspended. then, await-suspend expression evaluated. if expression has type bool , evaluates false , coroutine resumed. if expression exits via exception, exception caught, coroutine resumed, , exception re-thrown (15.1). otherwise, control flow returns current caller or resumer (8.4.4) without exiting scopes (6.6). —

so may resume coroutine thread, long guarantee exiting await-suspend not cause double resume, or destruction of coroutine


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -