C++ coroutines
As we have seen, coroutines are just functions, but they are not like the functions we are used to. They have special properties that we will study in this chapter. In this section, we will focus on coroutines in C++.
A function starts executing when it’s called and normally terminates with a return sentence or just when the function’s end is reached.
A function runs from beginning to end. It may call another function (or even itself if it is recursive), and it may throw exceptions or have different return points. But it always runs from beginning to end.
A coroutine is different. A coroutine is a function that can suspend itself. The flow for a coroutine may be like the following pseudocode:
void coroutine() { do_something(); co_yield; do_something_else(); co_yield; do_more_work(); co_return; }
We...