C++20 provides four features that change how we think about and write modern C++: concepts, the ranges library, coroutines, and modules. I already wrote a few posts to concepts and the ranges library. Let’s have a closer look at coroutines.
I want to use this post as a starting point to dive deeper into coroutines.
Coroutines are functions that can suspend and resume their execution while keeping their state. The evolution of functions goes in C++ one step further. What I present as a new idea in C++20 is quite old. Melvin Conway coined the term coroutine. He used it in his publication on compiler construction in 1963. Donald Knuth called procedures a particular case of coroutines.
With the new keywords co_await and co_yield, C++20 extends the execution of C++ functions with two new concepts.
- Thanks to co_await expression expression, it is possible to suspend and resume the execution of the expression. If you use the co_await expression in a function func, the call auto getResult = func() does not block if the function result is unavailable. Instead of resource-consuming blocking, you have resource-friendly waiting.
- co_yield expression expression allows it to write a generator function. The generator function returns a new value each time. A generator function is a data stream from which you can pick values. The data stream can be infinite. Consequentially, we are at the center of lazy evaluation.
Before I present a generator function to show the difference between a function and coroutines, I want to say a few words about the evolution of functions.
Evolution of Functions
The following code example shows the various simplified steps in the evolution of functions.
- Since the first C standard in 1972, we have functions: func1.
- With the first C++ standard in 1998, functions became way more powerful. We got
- Function overloading: func2.
- Function templates: func3.
- Function objects: func4. Often, they are erroneous, and called functors. Function objects are due to the overload call operator (operator ()) objects, which can be invoked. The second pair of round braces in line (1) represents the function call parameters.
- C++11 gave us lambda functions: func5.
- With C++14, lambda functions can be generic: func6.
Let’s go one step further. Generators are special coroutines.
In classical C++, I can implement a greedy generator.
A Greedy Generator
The following program is as straightforward as possible. The function getNumbers returns all integers from begin to end incremented by inc. begin has to be smaller than end, and inc has to be positive.
Of course, I am reinventing the wheel with getNumbers because that job could be done quite well with the algorithm std::iota. The output of the program is as expected.
Two observations of the program are essential. On the one hand, the vector numbers in line (1) always get all values. This holds even if I’m only interested in the first five elements of a vector with 1000 elements. On the other hand, it’s quite easy to transform the function getNumbers into a lazy generator.
A Lazy Generator
While the function getNumbers in the file greedyGenerator.cpp returns a std::vector, the coroutine generatorForNumbers in lazyGenerator.cpp returns a generator. The generator numbers in line (2) or generatorForNumbers(0, 5) in line (3) return a new number on request. The range-based for-loop triggers the query. To be more precise, the query of the coroutine returns the value i via co_yield i and immediately suspends its execution. If a new value is requested, the coroutine resumes its execution exactly at that place.
The expression generatorForNumbers(0, 5) in line (3) is a just-in-place usage of a generator. I want to stress one point explicitly. The coroutine generatorForNumbers creates an infinite data stream because the for-loop in line (3) has no end condition. This infinite data stream is fine if I only ask for a finite number of values, such as in line (4). This does not hold for line (3) since there is no end condition. Consequentially, the expression runs forever.
We don’t get C++20 concrete coroutines; we get a framework for writing our coroutines. You can assume that I have a lot to write about them.
First Virtual Meetup
I’m happy to give the first virtual talk for the C++ User Group in Munich. Here is the official invitation:
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, Marco Parri Empoli, moon, and Philipp Lenk.
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.
|My special thanks to Embarcadero|
|My special thanks to PVS-Studio|
|My special thanks to Tipi.build|
|My special thanks to Take Up Code|
|My special thanks to SHAVEDYAKS|
I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
- Embedded Programmierung mit modernem C++ 12.12.2023 – 14.12.2023 (Präsenzschulung, Termingarantie)
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
- C++ – The Core Language
- C++ – The Standard Library
- C++ – Compact
- C++11 and C++14
- Concurrency with Modern C++
- Design Pattern and Architectural Pattern with C++
- Embedded Programming with Modern C++
- Generic Programming (Templates) with C++
- Clean Code with Modern C++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- Mail: schulung@ModernesCpp.de
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org
Modernes C++ Mentoring,