An Introduction to C++ Coroutines

Learn about C++ coroutines, including coroutine abstraction, and learn to differentiate them from subroutines through examples.

A few motivating examples

Coroutines are one of those features, similar to lambda expressions, that offer a way to completely change the way we write and think about C++ code. The concept is very general and can be applied in many different ways. To give you a taste of how C++ can look when using coroutines, we will here look briefly at two examples.

Yield-expressions can be used for implementing generators—objects that produce sequences of values lazily. In this example, we will use the keywords co_yield and co_return to control the flow:

Get hands-on with 1200+ tech skills courses.