C++20: Coroutines – A First Overview

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.

 

Rainer D 6 P2 500x500Modernes C++ Mentoring

Be part of my mentoring programs:

  • "Fundamentals for C++ Professionals" (open)
  • "Design Patterns and Architectural Patterns with C++" (open)
  • "C++20: Get the Details" (open)
  • "Concurrency with Modern C++" (starts March 2024)
  • Do you want to stay informed: Subscribe.

     

    Evolution of Functions

    The following code example shows the various simplified steps in the evolution of functions.

    // functionEvolution.cpp
    
    int func1() {
        return 1972;
    }
    
    int func2(int arg) {
        return arg;
    }
    
    double func2(double arg) {
        return arg;
    }
    
    template <typename T>
    T func3(T arg) {
        return arg;
    }
    
    struct FuncObject4 {
        int operator()() { // (1)
            return 1998;
        }
    };
    
    auto func5 = [] {
        return 2011;
    };
    
    auto func6 = [] (auto arg){
        return arg;
    };
    
    int main() {
    
        func1();        // 1972
    
        func2(1998);    // 1998
        func2(1998.0);  // 1998.0
        func3(1998);    // 1998
        func3(1998.0);  // 1998.0
        FuncObject4 func4;
        func4();        // 1998
    
        func5();        // 2011
    
        func6(2014);    // 2014
        func6(2014.0);  // 2014
    
    }   
    
    • 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.

    Generators

    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.

    // greedyGenerator.cpp
    
    #include <iostream>
    #include <vector>
    
    std::vector<int> getNumbers(int begin, int end, int inc = 1) {
      
        std::vector<int> numbers;                      // (1)
        for (int i = begin; i < end; i += inc) {
            numbers.push_back(i);
        }
      
        return numbers;
      
    }
    
    int main() {
    
        std::cout << std::endl;
    
        const auto numbers= getNumbers(-10, 11);
      
        for (auto n: numbers) std::cout << n << " ";
      
        std::cout << "\n\n";
    
        for (auto n: getNumbers(0, 101, 5)) std::cout << n << " ";
    
        std::cout << "\n\n";
    
    }
    

    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.

    greedyGenerator

    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

    That’s all.

    // lazyGenerator.cpp
    
    #include <iostream>
    #include <vector>
    
    generator<int> generatorForNumbers(int begin, int inc = 1) {
      
      for (int i = begin;; i += inc) {
        co_yield i;
      }
      
    }
    
    int main() {
    
        std::cout << std::endl;
    
        const auto numbers= generatorForNumbers(-10);                   // (2)
      
        for (int i= 1; i <= 20; ++i) std::cout << numbers << " ";       // (4)
      
        std::cout << "\n\n";
                                                             
        for (auto n: generatorForNumbers(0, 5)) std::cout << n << " ";  // (3)
    
        std::cout << "\n\n";
    
    }
    

    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.

    What’s next?

    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:

    MUC

    Help us fight social isolation and join us next Thursday for our first-ever virtual meetup! @rainer_grimm will be talking about Concepts in C++20. March 26, 19:00 (CET).
    Check out the full event description at meetup.com/MUCplusplus. The stream is open for everyone; you don’t need to register on meetup for this one.

    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, 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, Philipp Lenk, Hobsbawm, and Charles-Jianye Chen.

    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

    Seminars

    I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.

    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++
    • C++20

    Online Seminars (German)

    Contact Me

    Modernes C++ Mentoring,

     

     

    0 replies

    Leave a Reply

    Want to join the discussion?
    Feel free to contribute!

    Leave a Reply

    Your email address will not be published. Required fields are marked *