Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a noop coroutine?

The C++20 standard defines a "noop coroutine" in [coroutine.noop]. What is it? And how is it different from a function whose body is { co_return; }?

UPDATE

Thank you for the links to the proposal and to the standard. To summarize from these references, a "noop coroutine" is one for which the resume() (or operator()) and destroy() methods have no side-effects.

But destroy() on a user-written coroutine always (I think!) has a side effect, namely destroying the coroutine and further calls to the coroutine handle methods including resume() and destroy() will now be undefined behavior, which is different than no-side-effects behavior.

In summary, there is no way to write a coroutine such that destroy() has no side effects, and only the library-provided noop coroutine has this property, correct?

(Updated answers welcome, in response to this update of my question.)

like image 623
cs- Avatar asked Nov 27 '25 11:11

cs-


1 Answers

This was proposed here: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0913r1.html

Basically, the idea is that

Having such a coroutine handle allows library writer to perform either symmetric or asymmetric control transfer based on runtime considerations.

Following code illustrates this idea (from the same proposal):

struct Awaiter {
   ...
   auto await_suspend(coroutine_handle<> h) {
     ...
     return cond ? next_coro : noop_coroutine(); 
   }
 };

This would not be necessarily different from hand-written noop coroutine, but having one already predefined for you is quite convenient.

like image 83
SergeyA Avatar answered Nov 29 '25 00:11

SergeyA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!