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: Meeting C++, Matt Braun, Roman Postanciuc, Venkata Ramesh Gudpati, Tobias Zindl, Marko, G Prvulovic, Reinhold Dröge, Abernitzke, Richard Ohnemus, Frank Grimm, Sakib, Broeserl, António Pina, Markus Falkner, Darshan Mody, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, espkk, and Wolfgang Gärtner.

 

Thanks in particular to:   crp4

 

   

Get your e-book at Leanpub:

The C++ Standard Library

 

Concurrency With Modern C++

 

Get Both as one Bundle

cover   ConcurrencyCoverFrame   bundle
With C++11, C++14, and C++17 we got a lot of new C++ libraries. In addition, the existing ones are greatly improved. The key idea of my book is to give you the necessary information to the current C++ libraries in about 200 pages. I also included more than 120 source files.  

C++11 is the first C++ standard that deals with concurrency. The story goes on with C++17 and will continue with C++20.

I'll give you a detailed insight into the current and upcoming concurrency in C++. This insight includes the theory and a lot of practice with more than 140 source files.

 

Get my books "The C++ Standard Library" (including C++17) and "Concurrency with Modern C++" in a bundle.

In sum, you get more than 700 pages full of modern C++ and more than 260 source files presenting concurrency in practice.

 

Get your interactive course

 

Modern C++ Concurrency in Practice

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

educative CLibrary

Based on my book "Concurrency with Modern C++" educative.io created an interactive course.

What's Inside?

  • 140 lessons
  • 110 code playgrounds => Runs in the browser
  • 78 code snippets
  • 55 illustrations

Based on my book "The C++ Standard Library" educative.io created an interactive course.

What's Inside?

  • 149 lessons
  • 111 code playgrounds => Runs in the browser
  • 164 code snippets
  • 25 illustrations

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
0 #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 5812

Yesterday 5047

Week 5812

Month 145573

All 4595665

Currently are 189 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments