You are on page 1of 91

Slides

Section : Coroutines
1
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

2
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
C++20 Coroutines

3
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Concepts Ranges

Coroutines Modules

4
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
• Coroutines are a generalization of functions in C++
• They are designed to make writing asynchronous
code much easier

5
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Function

• Can be called
• Can return something

6
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Coroutine

• Can be called
• Can return something
• Can be paused
• Can be resumed

7
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
8
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
C++ user code

Coroutine types generator<T>

Coroutine Infrastructure C++20

9
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Lazy computations

10
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

11
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Coroutine workflow

12
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Functions

13
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Call stack

14
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Coroutines

15
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Coroutines : call stack [ pause –resume]

16
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

17
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Coroutine keywords

18
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
19
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
20
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Coroutine keywords can’t show up in these functions
21
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
co_yield

22
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
co_yield

23
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
co_return

24
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
co_await

25
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
So how do we actually run this code?

26
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
C++ user code

Coroutine types

Coroutine Infrastructure

27
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

28
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Coroutine Infrastructure

29
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
30
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
C++ user code

Coroutine types generator<T>

Coroutine Infrastructure C++20

31
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Promise type Coroutine handle Awaiter

32
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
33
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Method Description
get_return_object(){} Return value of coroutine type

initial_suspend(){} Whether function is suspended upon


call
final_suspend(){} Whether coroutine state is destroyed
at last suspension point
Unhandled_exception(){} Handling exceptions thrown into
coroutines
Return_value(value){} Enables the co_return v; syntax
Return_void(){} Enables the co_return; syntax
Yield_value(value){} Enables the co_yield v; syntax

34
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Method Description
bool await_ready(){} Whether the co_await expression suspends.
If false is returned, then await_suspend is
called, to (mostly) suspend
void await_suspend(){} May suspend the coroutine, or schedule the
coroutine state for destruction
void await_resume(){} May return the result of the entire co_await
expression

35
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Awaitables

36
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Awaiter

It is possible to create your own awaiters, things that can act as


operands to the co_await operator, by setting up structs/classes that
overload the co_await operator

37
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Custom Awaitables

38
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
39
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
• C++ 20 doesn’t provide actual usable coroutine types like CoroType

• It provides the low level infrastructure to build them (promises, awaitables,


coroutine handles,…

• Building your own coroutine types is not recommended. It’s only reserved
for hard core, highly experienced library developers who really know what
they’re doing

• It is expected that C++23 will provide high level coroutine types built into
C++, ready to use just by including some headers

• If you want to use them know, there are third party libraries that can help,
like cppcoro and some others

40
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

41
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
co_await

42
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
C++ user code

Coroutine types CoroType

Coroutine Infrastructure C++20

43
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Keep in mind

• What we’re going to do here works for modern compilers with C++
20 enabled and support for coroutines

• gcc and msvc have been tested :


• gcc 10 and gcc 11 require the –fcoroutines switch
• msvc 2019 (version 16.8.3) and upwards don’t require any switch

• Online compilers are also a convenient option :


• https://wandbox.org is a good choice

44
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
45
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Awaitables

46
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Putting it all together

47
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

48
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
co_yield

49
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
C++ user code

Coroutine types generator<T>

Coroutine Infrastructure C++20

50
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
51
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
co_yield 2; co_await promise.yield_value(2);

52
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Awaitables

53
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Awaiter

It is possible to create your own awaiters, things that can act as


operands to the co_await operator, by setting up structs/classes that
overload the co_await operator

54
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Custom Awaitables

55
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Putting it all together

56
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

57
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
co_return

58
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
C++ user code

Coroutine types generator<T>

Coroutine Infrastructure C++20

59
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
60
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Putting it all together

61
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
• C++ 20 doesn’t provide actual usable coroutine types like generator<T>

• It provides the low level infrastructure to build them (promises, coroutine


frames, coroutine handles,…

• Building your own coroutine types is not recommended. It’s only reserved
for hard core, highly experienced library developers who really know what
they’re doing

• It is expected that C++23 will provide high level coroutine types built into
C++, ready to use just by including some headers

• If you want to use them know, there are third party libraries that can help,
like cppcoro

• Before we use cppcoro though, we need to build it and load it into our
C++ project

• In the next chapter, we explore basic info and knowledge you need to 62
create: and
The C++ 20 Masterclass use third party
From Fundamentals libraries in
to Advanced C++, Gakwaya
© Daniel like cppcoro
Slide intentionally left empty

63
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Custom coroutine types :
Generator

64
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
65
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
C++ user code

Coroutine types

Coroutine Infrastructure

66
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
C++ user code

Coroutine types generator<T>

Coroutine Infrastructure C++20

67
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
68
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
69
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
70
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
71
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

72
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Third Party Coroutine Types

73
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
C++ user code

Coroutine types generator<T>

Coroutine Infrastructure C++20

74
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
• Cppcoro
• https://github.com/Quuxplusone/coro

75
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
#include “third_party_coro_type.h”

76
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
77
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
78
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
79
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
No function call operator in <unique_generator>

80
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Changing the interface of unique_generator

81
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

82
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Coroutines : Summary

83
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
C++ user code

Coroutine types generator<T>

Coroutine Infrastructure C++20

84
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
• co_await
• co_yield
• co_return

85
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
86
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
87
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
88
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
89
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
• C++20 doesn’t provide actual coroutine types for direct use

• It only provides an infrastructure to build them

• Good coroutine types are hard to build and get right

• It’s advised to at least start by using third party coroutine type


libraries and only build your own when you REALLY know what
you’re doing

90
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

91
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya

You might also like