C++20: Coroutines - A First Overview

Contents[Show]

C++20 provides four features that change the way 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. 

 

TimelineCpp20

 

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 special 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 an expression. If you use co_await expression in a function func, the call auto getResult = func() does not block if the result of the function is not available. 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 kind of data stream from which you can pick values. The data stream can be infinite. Consequentially, we are in 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. 

 

// 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 become way more powerful. We got
    • Function overloading: func2.
    • Function templates: func3.
    • Function objects: func4. Often, they are erroneous, 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) stands for 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 good 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 gets 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) returns 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 with 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, Marko, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Darshan Mody, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, espkk, Wolfgang Gärtner,  Louis St-Amour, Stephan Roslen, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Avi Kohn, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Neil Wang, Friedrich Huber, Sudhakar Balagurusamy, lennonli, and Pramod Tikare Muralidhara.

 

Thanks in particular to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, and Dendi Suhubdy

 

Seminars

I'm happy to give online-seminars or face-to-face seminars world-wide. Please call me if you have any questions.

Bookable (Online)

Deutsch

English

Standard Seminars 

Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.

New

Contact Me

Modernes C++,

RainerGrimmSmall

Comments   

+1 #1 Bobur 2020-03-31 04:11
How can I compile "LazyGenerator.cpp"?
I tried using clang 10.0 with options "-std=c++2a", #included . But it cannot recognize "generator". Any ideas, please?
Quote
0 #2 Bobur 2020-04-01 06:56
Quoting Bobur:
How can I compile "LazyGenerator.cpp"?
I tried using clang 10.0 with options "-std=c++2a", #included "experimental/coroutine". But it cannot recognize "generator". Any ideas, please?


Sorry... This comment was about your second article on Coroutines.
Quote
-1 #3 Rainer Grimm 2020-04-02 06:12
Quoting Bobur:
How can I compile "LazyGenerator.cpp"?
I tried using clang 10.0 with options "-std=c++2a", #included . But it cannot recognize "generator". Any ideas, please?

I define the coroutine in a further post, in which I write about the coroutine framework.
Quote

My Newest E-Books

Course: Modern C++ Concurrency in Practice

Course: C++ Standard Library including C++14 & C++17

Course: Embedded Programming with Modern C++

Course: Generic Programming (Templates)

Course: C++ Fundamentals for Professionals

Subscribe to the newsletter (+ pdf bundle)

Blog archive

Source Code

Visitors

Today 2646

Yesterday 7515

Week 34257

Month 192688

All 5062002

Currently are 147 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments