Professional Documents
Culture Documents
Section Cpp20 Coroutines Slides
Section Cpp20 Coroutines 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
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
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
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
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
• 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
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
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
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
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
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>
• 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
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
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
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
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