Coroutines
Before we start implementing coroutines in C++, we will introduce coroutines conceptually and see how they can be useful in our programs.
Let’s start with a definition. A coroutine is a function that can suspend itself. Coroutines suspend themselves while waiting for an input value (while they are suspended, they don’t execute) or after yielding a value such as the result of a computation. Once the input value is available or the caller requests another value, the coroutine resumes execution. We will come back to coroutines in C++ soon, but let’s see with a real-life example how a coroutine works.
Imagine someone working as an assistant. They start the day reading emails.
One of the emails is a request for a report. After reading the email, they start writing the requested document. Once they have written the introductory paragraphs, they notice that they need another report from a colleague to get some accounting results from the previous quarter...