-
Notifications
You must be signed in to change notification settings - Fork 1
/
coroutines-ts.tex
691 lines (561 loc) · 21.3 KB
/
coroutines-ts.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
% !TeX Options = -shell-escape
\documentclass[10pt]{beamer}
\usetheme[progressbar=frametitle, block=fill]{metropolis}
\usepackage{xcolor}
\usepackage{multirow}
\usepackage{pgfpages}
\usepackage{pifont}
\usepackage[export]{adjustbox}
\usepackage{hyperref}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\newcommand{\cmark}{\ding{51}}
\newcommand{\xmark}{\ding{55}}
\setbeamertemplate{note page}{\insertnote}
%\setbeameroption{show notes on second screen=left}
\setbeameroption{hide notes}
\definecolor{amethyst}{rgb}{0.5, 0.4, 1.0}
\definecolor{amethystgrey}{rgb}{0.85, 0.85, 1.0}
\definecolor{amethystdark}{rgb}{0.4, 0.3, 0.9}
\definecolor{orangedark}{rgb}{0.0, 0.9, 0}
%\definecolor{titlebg}{HTML}{4e8074}
%\definecolor{titlebg}{HTML}{3e7985}
\definecolor{titlebg}{HTML}{fbf8ff}
\definecolor{font}{HTML}{23373b}
%\setbeamercolor{title}{fg=amethyst, bg=amethyst}
\setbeamercolor{frametitle}{fg= font, bg=titlebg}
%\setbeamercolor{section title}{black}
%\setbeamercolor{structure}{fg=amethyst, bg=amethyst}
\setbeamercolor{progress bar}{ fg = amethyst, bg= amethystgrey }
%\setbeamercolor{itemize item}{fg=amethyst,bg=white}
\setbeamercolor{alerted text}{fg=amethystdark}
%\setbeamercolor{title separator}{ ... }
%\setbeamercolor{progress bar in head/foot}{ ... }
%\setbeamercolor{progress bar in section page}{ ... }
\newcommand{\code}[1]{\texttt{#1}}
\usepackage{booktabs}
\usepackage[scale=2]{ccicons}
\usepackage{minted}
\usepackage{pgfplots}
\usepackage{xspace}
\title{Coroutines}
\subtitle{All you need to know about the coroutines}
\date{}
\author{Dawid Pilarski}
\institute{dawid.pilarski@panicsoftware.com \\ blog.panicsoftware.com}
\setminted[c++]
{
framesep=2mm,
baselinestretch=1.2,
fontsize=\footnotesize,
autogobble=true
}
\begin{document}
\maketitle
\begin{frame}{Agenda}
\tableofcontents
\end{frame}
\section{Coroutine theory - what are the coroutines?}
\begin{frame}{What are the coroutines?}
\alert{Coroutines} are \alert{generalization} of the function, that can be:
\begin{itemize}[<+- |alert@+>]
\item called
\item returned from
\item created
\item destroyed
\item suspended
\item resumed
\end{itemize}
\end{frame}
\begin{frame}{Coroutine flowchart}
\begin{columns}
\begin{column}{0.48\linewidth}
\centering
Function's flow:
\vskip 2em
\includegraphics[width=0.8\linewidth]{graphics/function-call.png}
\end{column}
\begin{column}{0.48\linewidth}
\centering
Coroutine flow:
\vskip 2em
\includegraphics[width=0.8\linewidth]{graphics/coroutine-workflow.png}
\end{column}
\end{columns}
\end{frame}
\begin{frame}{What are coroutines for?}
\centerline{Common use cases for the coroutines are:}
\begin{itemize}[<+- |alert@+>]
\item lazy computation of the sequences (generators)
\item lightweight (cooperative) "concurrency"
\end{itemize}
\end{frame}
\begin{frame}{Example coroutines usage}
\includegraphics[max size={\textwidth}{\textheight}]{graphics/coro-example-scheduler/coro-0.png}
\end{frame}
\begin{frame}{Example coroutines usage}
\includegraphics[max size={\textwidth}{\textheight}]{graphics/coro-example-scheduler/coro-1.png}
\end{frame}
\begin{frame}{Example coroutines usage}
\includegraphics[max size={\textwidth}{\textheight}]{graphics/coro-example-scheduler/coro-2.png}
\end{frame}
\begin{frame}{Example coroutines usage}
\includegraphics[max size={\textwidth}{\textheight}]{graphics/coro-example-scheduler/coro-3.png}
\end{frame}
\begin{frame}{Example coroutines usage}
\includegraphics[max size={\textwidth}{\textheight}]{graphics/coro-example-scheduler/coro-4.png}
\end{frame}
\begin{frame}{Example coroutines usage}
\includegraphics[max size={\textwidth}{\textheight}]{graphics/coro-example-scheduler/coro-5.png}
\end{frame}
\begin{frame}{Example coroutines usage}
\includegraphics[max size={\textwidth}{\textheight}]{graphics/coro-example-scheduler/coro-6.png}
\end{frame}
\begin{frame}[fragile]{The state}
\centerline{Imagine you want to suspend a function:}
\vfill
\begin{columns}
\begin{column}{0.48\linewidth}
\begin{minted}[autogobble=false]{c++}
int foo(){
int a = 1;
co_yield a++;
co_yield a++;
co_yield a++;
}
\end{minted}
\end{column}
\pause
\begin{column}{0.48\linewidth}
\centerline{issues:}
\begin{itemize}[<+- |alert@+>]
\item on suspension variables needs to be saved somewhere
\item coroutine needs to know where it suspended last time
\end{itemize}
\end{column}
\end{columns}
\end{frame}
\begin{frame}[fragile]{How to achieve that?}
\begin{minted}[autogobble=false]{c++}
struct foo_state{
int a=1;
int recent_point=0;
};
int foo(foo_state* state){
if(state.recent_point == 0) goto recent_point_0;
if(state.recent_point == 1) goto recent_point_1;
if(state.recent_point == 2) goto recent_point_2;
recent_point_0:
return state.a++;
recent_point_1:
return state.a++;
recent_point_2:
return state.a++;
}
\end{minted}
\end{frame}
\begin{frame}{Possible coroutines implementations}
\begin{columns}[t]
\begin{column}{0.48\linewidth}
\centering Language based
\vskip 2em
\includegraphics[width=.8\linewidth]{graphics/cppcoro-creation.png}
\end{column}
\begin{column}{0.48\linewidth}
\centering
Library based
\vskip 2em
\includegraphics[width=.8\linewidth]{graphics/coroutine-interaction.png}
\end{column}
\end{columns}
\end{frame}
\begin{frame}{Closer look into Boost.Fiber}
\begin{itemize}[<+- |alert@+>]
\item Need to allocate the stack for the Fiber/Coroutine
\item Can be suspended from the top level functions and below
\item Allocation of the memory in advance
\item Hard to optimize by compilers
\end{itemize}
\end{frame}
\begin{frame}{Closer look into built-in coroutines}
\begin{columns}[t]
\begin{column}{0.48\linewidth}
\centerline{Boost.Fiber}
\vfill
\begin{itemize}
\item Need to allocate the stack for the Fiber/Coroutine
\item Can be suspended from the top level functions and below
\item Allocation of the memory in advance
\item Hard to optimize by compilers
\end{itemize}
\end{column}
\begin{column}{0.48\linewidth}
\centerline{\alert{built-in coroutines}}
\vfill
\begin{itemize}[<+- |alert@+>]
\item Need to allocate the frame for the Coroutine
\item Can be suspended only from the top level functions
\item Minimal memory allocation
\item Easy to optimize by compilers
\item Ends up with lots of allocations
\end{itemize}
\end{column}
\end{columns}
\end{frame}
\begin{frame}[fragile]{Coroutine declarations}
\centering Same as functions
\vfill
\begin{center}
\begin{minipage}{0.7\linewidth}
\inputminted{c++}{code-examples/intro/declaration.hpp}
\end{minipage}
\end{center}
\vfill
Whether the function is a coroutine depends on \alert{it's definition}.
\end{frame}
\begin{frame}{3 new keywords}
\begin{description}
\item [\code{\alert{co\_return}}] \hfill \\ Returning (or not) value and finishing the coroutine
\item [\code{\alert{co\_yield}}] \hfill \\ Returning intermediate value from the coroutine
\item [\code{\alert{co\_await}}] \hfill \\ Awaiting completion of the "task"
\end{description}
\end{frame}
\section{Practical part I - using folly/coro library}
\begin{frame}{generators - Fibonacci sequence}
\only{\inputminted[firstline=5, highlightlines={5}]{c++}{code-examples/cppcoro/generator.cpp}}<1>
\only{\inputminted[firstline=5, highlightlines={6,7}]{c++}{code-examples/cppcoro/generator.cpp}}<2>
\only{\inputminted[firstline=5, highlightlines={9,14}]{c++}{code-examples/cppcoro/generator.cpp}}<3>
\only{\inputminted[firstline=5, highlightlines={10}]{c++}{code-examples/cppcoro/generator.cpp}}<4>
\only{\inputminted[firstline=5, highlightlines={11-13}]{c++}{code-examples/cppcoro/generator.cpp}}<5>
\only{\inputminted[firstline=5, highlightlines={16}]{c++}{code-examples/cppcoro/generator.cpp}}<6>
\end{frame}
\begin{frame}{generators - excercise}
\centering Implement
\vskip 1em
\centering triangular number series \\ \vskip 2em
\includegraphics[width=0.5\linewidth]{graphics/triangular-series.png}
\vfill
\footnotesize \href{https://edublognss.wordpress.com/2013/04/16/famous-mathematical-sequences-and-series/}{\alert{source}}
\end{frame}
\begin{frame}{tasks and events}
\only{\inputminted[firstline=47, lastline=68, highlightlines={47, 48}]{c++}{code-examples/cppcoro/events.cpp}}<1>
\only{\inputminted[firstline=47, lastline=68, highlightlines={49, 66}]{c++}{code-examples/cppcoro/events.cpp}}<2>
\only{\inputminted[firstline=47, lastline=68, highlightlines={50, 57, 58, 66}]{c++}{code-examples/cppcoro/events.cpp}}<3>
\only{\inputminted[firstline=47, lastline=68, highlightlines={51, 56}]{c++}{code-examples/cppcoro/events.cpp}}<4>
\only{\inputminted[firstline=47, lastline=68, highlightlines={52}]{c++}{code-examples/cppcoro/events.cpp}}<5>
\only{\inputminted[firstline=47, lastline=68, highlightlines={53}]{c++}{code-examples/cppcoro/events.cpp}}<6>
\only{\inputminted[firstline=47, lastline=68, highlightlines={54, 55}]{c++}{code-examples/cppcoro/events.cpp}}<7>
\only{\inputminted[firstline=47, lastline=68, highlightlines={59, 65}]{c++}{code-examples/cppcoro/events.cpp}}<8>
\only{\inputminted[firstline=47, lastline=68, highlightlines={60-62}]{c++}{code-examples/cppcoro/events.cpp}}<9>
\only{\inputminted[firstline=47, lastline=68, highlightlines={63,64}]{c++}{code-examples/cppcoro/events.cpp}}<10>
\end{frame}
\begin{frame}{other (for now only msvc)}
\begin{itemize}
\item mutexes
\item file I/O operations
\item networking operations
\end{itemize}
\end{frame}
\section{Theory - promise\_types}
\begin{frame}{promise\_type}
\begin{itemize}[<+- |alert@+>]
\item promise\_type is responsible for coroutine's behavior:
\begin{itemize}
\item on coroutine's start and stop
\item on throwing unhandled exception
\item on returning the value
\item on yielding the value
\item partially on waiting for the task's completion
\end{itemize}
\item promise\_type is strongly connected with coroutine's returned type
\begin{itemize}[<+- |alert@+>]
\item it can be a member of the returned type \code{returned\_type::promise\_type}
\item it can be defined as \code{std::coroutine\_traits<returned\_type>::promise\_type}
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}{coroutine body}
\centerline{Each time we write coroutine, compiler modifies it's body into following form:}
\only{\inputminted[firstline=1, highlightlines={2}]{c++}{code-examples/theory-custom-coroutine/coroutine-body.cpp}}<1>
\only{\inputminted[firstline=1, highlightlines={3}]{c++}{code-examples/theory-custom-coroutine/coroutine-body.cpp}}<2>
\only{\inputminted[firstline=1, highlightlines={4}]{c++}{code-examples/theory-custom-coroutine/coroutine-body.cpp}}<3>
\only{\inputminted[firstline=1, highlightlines={6,8,10}]{c++}{code-examples/theory-custom-coroutine/coroutine-body.cpp}}<4>
\only{\inputminted[firstline=1, highlightlines={7}]{c++}{code-examples/theory-custom-coroutine/coroutine-body.cpp}}<5>
\only{\inputminted[firstline=1, highlightlines={9}]{c++}{code-examples/theory-custom-coroutine/coroutine-body.cpp}}<6>
\only{\inputminted[firstline=1, highlightlines={12,13}]{c++}{code-examples/theory-custom-coroutine/coroutine-body.cpp}}<7>
\end{frame}
\begin{frame}{promise\_type extensions}
\centerline{We can extend the coroutine body with:}
\begin{itemize}
\item (mandatory) support for \alert{returning void} or \alert{returning value}
\item custom memory allocation algorithm (custom \code{operator new})
\item (optional) support for \alert{yielding intermediate values}
\end{itemize}
\end{frame}
\begin{frame}[fragile]{co\_return - support for returning from coroutine}
\centerline{\alert{\code{co\_return}}}
\begin{columns}[T]
\begin{column}{0.48\linewidth}
\vskip 2em
\centerline{Usage: }
\vskip 2em
\begin{onlyenv}<1>
without expression:
\begin{minted}{c++}
co_return
\end{minted}
with void expression:
\begin{minted}{c++}
co_return <void expression>
\end{minted}
\end{onlyenv}
\begin{onlyenv}<2>
with non-void expression:
\begin{minted}{c++}
co_return <expression>
\end{minted}
\end{onlyenv}
\end{column}
\begin{column}{0.48\linewidth}
\vskip 2em
\centerline{Translated to: }
\vskip 2em
\begin{onlyenv}<1>
\begin{minted}{c++}
<expression>;
promise.return_void();
goto final_suspend;
\end{minted}
\end{onlyenv}
\begin{onlyenv}<2>
\begin{minted}{c++}
promise.return_value(<expression>);
goto final_suspend;
\end{minted}
\end{onlyenv}
\end{column}
\end{columns}
\end{frame}
\begin{frame}[fragile]{co\_yield - support for returning intemediate values}
\centerline{\alert{\code{co\_yield}}}
\begin{columns}
\begin{column}{0.48\linewidth}
\vskip 2em
Usage:
\vskip 2em
\begin{minted}{c++}
co_yield <non-void expression>
\end{minted}
\end{column}
\begin{column}{0.48\linewidth}
\vskip 2em
Translated to:
\vskip 2em
\begin{minted}{c++}
co_await promise.
yield_value(<expression>)
\end{minted}
\end{column}
\end{columns}
\end{frame}
\begin{frame}{co\_await shortly}
\begin{description}
\item[\code{co\_await std::experimental::suspend\_always\{\}}] - \hfill \\ suspends the coroutine
\item[\code{co\_await std::experimental::suspend\_never\{\}}] - \hfill \\ does nothing
\end{description}
\end{frame}
\begin{frame}{Ok, ok but how do I even resume the coroutine?}
\centering The low-level interface to the type-erased coroutine is \code{\alert{coroutine\_handle}} object.
\only{\inputminted[highlightlines={1}]{c++}{code-examples/theory-custom-coroutine/coroutine-handle.h}}<1>
\only{\inputminted[highlightlines={2,3}]{c++}{code-examples/theory-custom-coroutine/coroutine-handle.h}}<2>
\only{\inputminted[highlightlines={4}]{c++}{code-examples/theory-custom-coroutine/coroutine-handle.h}}<3>
\only{\inputminted[highlightlines={6,7}]{c++}{code-examples/theory-custom-coroutine/coroutine-handle.h}}<4>
\only{\inputminted[highlightlines={9, 10}]{c++}{code-examples/theory-custom-coroutine/coroutine-handle.h}}<5>
\only{\inputminted[highlightlines={12, 13}]{c++}{code-examples/theory-custom-coroutine/coroutine-handle.h}}<6>
\only{\inputminted[highlightlines={15}]{c++}{code-examples/theory-custom-coroutine/coroutine-handle.h}}<7>
\end{frame}
\begin{frame}{And how do I get the coroutine\_handle object?}
\centerline{\code{\alert{coroutine\_handles}} are specialized for \code{\alert{promise\_type}}}
\only{\inputminted[highlightlines={1}]{c++}{code-examples/theory-custom-coroutine/coroutine-handle-promise.h}}<1>
\only{\inputminted[highlightlines={2}]{c++}{code-examples/theory-custom-coroutine/coroutine-handle-promise.h}}<2>
\only{\inputminted[highlightlines={4}]{c++}{code-examples/theory-custom-coroutine/coroutine-handle-promise.h}}<3>
\only{\inputminted[highlightlines={5, 10}]{c++}{code-examples/theory-custom-coroutine/coroutine-handle-promise.h}}<4>
\only{\inputminted[highlightlines={6, 8}]{c++}{code-examples/theory-custom-coroutine/coroutine-handle-promise.h}}<5>
\end{frame}
\section{Promise\_type excercise}
\begin{frame}{promise\_type excercise - lazy}
\centerline{type for lazy initialization}
\vfill
requirements:
\begin{itemize}
\item synchronous (no support for multithreading + no support for co\_await).
\item no sharing of the value (no copy, only move constructor).
\item interface simillar to the std::optional.
\end{itemize}
\end{frame}
\begin{frame}{promise\_type excercise - generator}
\centerline{type for generating sequences}
\vfill
requirements:
\begin{itemize}
\item synchronous (no support for multithreading + no support for co\_await).
\item next method should return the value and resume the coroutine.
\item interface simillar to the std::optional
\end{itemize}
\end{frame}
\section{Asynchronous coroutines}
\begin{frame}{co\_await}
\centerline{\alert{co\_await}}
\begin{itemize}
\item represents awaiting for operations' completion
\item it's argument is (usually) called awaitable
\item it's result is usually called awaiter
\end{itemize}
\end{frame}
\begin{frame}{co\_await translation}
\centerline{If compiler meets the co\_await it gets translated into following code:}
\only{\inputminted[highlightlines={3, 7}]{c++}{code-examples/theory-custom-coroutine/co-await-transformation.cpp}}<1>
\only{\inputminted[highlightlines={9, 12}]{c++}{code-examples/theory-custom-coroutine/co-await-transformation.cpp}}<2>
\only{\inputminted[highlightlines={4}]{c++}{code-examples/theory-custom-coroutine/co-await-transformation.cpp}}<3>
\only{\inputminted[highlightlines={6, 2}]{c++}{code-examples/theory-custom-coroutine/co-await-transformation.cpp}}<4>
\only{\inputminted[highlightlines={12}]{c++}{code-examples/theory-custom-coroutine/co-await-transformation.cpp}}<5>
\only{\inputminted[highlightlines={10, 11}]{c++}{code-examples/theory-custom-coroutine/co-await-transformation.cpp}}<6>
\end{frame}
\begin{frame}[fragile]{Await suspend}
% nothing yet said about the coroutine_handle
\centerline{await suspend is of the following form:}
\begin{minted}[autogobble=false]{c++}
awaitable.await_suspend(this_coroutine_handle);
\end{minted}
\centerline{\code{await\_suspend} might return:}
\vskip 1em
\hrule
\begin{columns}
\begin{column}{0.35\linewidth}
\begin{itemize}[<+- |alert@+>]
\item \code{void}
\item \code{bool}
\item \code{coroutine\_handle}
\end{itemize}
\end{column}
\begin{column}{0.66\linewidth}
\begin{onlyenv}<1>
\inputminted[firstline=1, lastline=9, linenos=false]{c++}{code-examples/theory-custom-coroutine/co-await-transformation-suspend.cpp}
\end{onlyenv}
\begin{onlyenv}<2>
\inputminted[firstline=10, lastline=20, linenos=false]{c++}{code-examples/theory-custom-coroutine/co-await-transformation-suspend.cpp}
\end{onlyenv}
\begin{onlyenv}<3>
\inputminted[firstline=21, linenos=false]{c++}{code-examples/theory-custom-coroutine/co-await-transformation-suspend.cpp}
\end{onlyenv}
\end{column}
\end{columns}
\end{frame}
\begin{frame}[fragile]{Awaitable and Awaiter}
\alert{Awaitable} is an object, which is an operand of the co\_await operator
\alert{co\_await <expression>} expression will be processed in following manner
\vskip 2em
\begin{columns}
\begin{column}{0.4\linewidth}
\begin{itemize}[<+- |alert@+>]
\item await\_transform
\item acquiring awaiter
\begin{itemize}[<+- |alert@+>]
\item co\_await operator
\item global co\_await operator
\item awaitable to awaiter
\end{itemize}
\end{itemize}
\end{column}
\begin{column}{0.56\linewidth}
\footnotesize
\begin{onlyenv}<1>
is performed only if promise has await\_transform function declared
\vskip 2em
\begin{minted}{c++}
co_await promise.await_transform(<expr>);
\end{minted}
\end{onlyenv}
\begin{onlyenv}<3>
is performed only if awaitable has co\_await operator
\begin{minted}{c++}
auto&& awaiter =
<awaitable>.operator co_await();
\end{minted}
\end{onlyenv}
\begin{onlyenv}<4>
is performed only if awaitable there is matching global co\_await operator
\begin{minted}{c++}
auto&& awaiter =
operator co_await(<awaitable>);
\end{minted}
\end{onlyenv}
\begin{onlyenv}<5>
\begin{minted}{c++}
auto&& awaiter = <awaitable>;
\end{minted}
\end{onlyenv}
\end{column}
\end{columns}
\end{frame}
\section{Asynchronous coroutines - excercises}
\begin{frame}{What is the task?}
\only{\inputminted[firstline=4, highlightlines={4}]{c++}{code-examples/cppcoro/io.cpp}}<1>
\only{\inputminted[firstline=4, highlightlines={6}]{c++}{code-examples/cppcoro/io.cpp}}<2>
\only{\inputminted[firstline=4, highlightlines={8-11}]{c++}{code-examples/cppcoro/io.cpp}}<3>
\only{\inputminted[firstline=4, highlightlines={12,17}]{c++}{code-examples/cppcoro/io.cpp}}<4>
\only{\inputminted[firstline=4, highlightlines={14}]{c++}{code-examples/cppcoro/io.cpp}}<5>
\only{\inputminted[firstline=4, highlightlines={15-16}]{c++}{code-examples/cppcoro/io.cpp}}<6>
\end{frame}
\begin{frame}{task}
\centerline{type for asynchronous operations}
\vfill
requirements:
\begin{itemize}
\item single-threaded
\item asynchronous (support for the co\_await)
\item coroutine after finishing must resume the co\_awaiting coroutine
\item some kind of the executor needed to start the coroutines
\end{itemize}
\end{frame}
\begin{frame}{event}
\centerline{type for communication of the tasks}
\vfill
requirements:
\begin{itemize}
\item stores information whether the event is set.
\item stores the continuation object.
\item launches the continuation on setting up the event.
\end{itemize}
\end{frame}
\begin{frame}{Thank you!}
\centerline{Questions?}
\vfill
\begin{columns}[t]
\footnotesize
\begin{column}{0.48\linewidth}
\centerline{recommended lecture:}
\vskip 0.5em
\hrule
\begin{itemize}
\item \href{https://lewissbaker.github.io/}{\alert{Lewiss Baker blog}}
\item \href{https://blog.panicsoftware.com/}{\alert{My blog}}
\item \href{https://programistamag.pl/}{\alert{"programista" magazine}}
\item \href{eel.is/c++draft/}{\alert{current C++ standard draft}}
\item {coroutine channel on cpplang slack}
\end{itemize}
\end{column}
\begin{column}{0.48\linewidth}
\centerline{recommended videos:}
\vskip 0.5em
\hrule
\begin{itemize}
\item \href{https://www.youtube.com/watch?v=8C8NnE1Dg4A}{\alert{Gor Nishanov \\ "C++ Coroutines: Under the covers"}}
\item \href{https://www.youtube.com/watch?v=mlP1MKP8d_Q}{\alert{Toby Allsopp \\ "Coroutines: what can't they do"}}
\item \href{https://www.youtube.com/watch?v=ZTqHjjm86Bw}{\alert{James McNellis \\ "Introduction to C++ Coroutines"}}
\end{itemize}
\end{column}
\end{columns}
\end{frame}
\end{document}