Repository navigation
Expand file tree
/
Copy pathTheArtOfWritingReasonableConcurrentCode.tex
More file actions
1402 lines (1114 loc) · 45.2 KB
/
Copy pathTheArtOfWritingReasonableConcurrentCode.tex
File metadata and controls
1402 lines (1114 loc) · 45.2 KB
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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%\documentclass[handout]{beamer}
%\documentclass{beamer}
\documentclass[aspectratio=169]{beamer}
\mode<presentation>
{
\usetheme[hideothersubsections]{Goettingen}
\usecolortheme{beaver}
}
\usepackage{fontenc}
\usepackage{multirow}
\usepackage{multicol}
\usepackage[export]{adjustbox}
\usepackage{listings}
\usepackage{pgf, pgffor}
\usepackage{lstlinebgrd} % see http://www.ctan.org/pkg/lstaddons
\usepackage{caption}
\usepackage{graphicx}
\usepackage{pifont}% http://ctan.org/pkg/pifont
\usepackage{xcolor,colortbl}
\usepackage{hyperref}
%\usepackage{pgfplots}
\usepackage{tcolorbox}
\usepackage{pgfpages}
\usepackage{savesym}
\usepackage{amsmath}
\savesymbol{checkmark}
\usepackage{dingbat}
\title[The Art of Writing Reasonable Concurrent Code]{The Art of Writing Reasonable Concurrent Code\\ {\small Pre-Conference Workshop ACCU 2017} \\ {\footnotesize \copyright 2017}}
\author{Felix Petriconi}
\date{2017-04-25}
% -----------------------------------------------------------------------------------------
% custom makros
% -----------------------------------------------------------------------------------------
\newcommand{\cppkey}[1]{\texttt{\textcolor{blue}{#1}}}
\newcommand{\cmark}{\textcolor{green}{\ding{51}}}
\newcommand{\xmark}{\textcolor{red}{\ding{55}}}
\newcommand{\outputbox}[1]{%
\begin{tcolorbox}[colback=gray!5,colframe=gray!40!black,title=Output]%
\begin{flushleft}%
{\tiny #1}%
\end{flushleft}%
\end{tcolorbox}%
}
% -----------------------------------------------------------------------------------------
\definecolor{mygreen}{rgb}{0,0.6,0}
\definecolor{mygray}{rgb}{0.5,0.5,0.5}
\definecolor{mymauve}{rgb}{0.58,0,0.82}
\definecolor{columnheadinggray}{rgb}{0.6,0.6,0.6}
\lstset{ %
backgroundcolor=\color{white}, % choose the background color; you must add \usepackage{color} or \usepackage{xcolor}
basicstyle=\ttfamily\tiny, % the size of the fonts that are used for the code
breakatwhitespace=false, % sets if automatic breaks should only happen at whitespace
breaklines=true, % sets automatic line breaking
captionpos=none, % sets the caption-position to top
commentstyle=\color{mygreen}, % comment style
deletekeywords={...}, % if you want to delete keywords from the given language
escapeinside={/*}{*/}, % if you want to add LaTeX within your code
extendedchars=true, % lets you use non-ASCII characters; for 8-bits encodings only, does not work with UTF-8
frame=single, % adds a frame around the code
keepspaces=true, % keeps spaces in text, useful for keeping indentation of code (possibly needs columns=flexible)
keywordstyle=\color{blue}, % keyword style
language=C++, % the language of the code
otherkeywords={alignas,alignof,
char16_t,char32_t,constexpr,
decltype,declval,noexcept,
nullptr,static_assert,
thread_local,override,final}, % if you want to add more keywords to the set
numbers=left, % where to put the line-numbers; possible values are (none, left, right)
numbersep=5pt, % how far the line-numbers are from the code
numberstyle=\tiny\color{mygray}, % the style that is used for the line-numbers
rulecolor=\color{black}, % if not set, the frame-color may be changed on line-breaks within not-black text (e.g. comments (green here))
showspaces=false, % show spaces everywhere adding particular underscores; it overrides 'showstringspaces'
showstringspaces=false, % underline spaces within strings only
showtabs=false, % show tabs within strings adding particular underscores
stepnumber=1, % the step between two line-numbers. If it's 1, each line will be numbered
stringstyle=\color{mymauve}, % string literal style
tabsize=2, % sets default tabsize to 2 spaces
title=\lstname % show the filename of files included with \lstinputlisting; also try caption instead of title
}
\lstdefinestyle{CodeInTextStyle} {
basicstyle=\ttfamily\normalsize,
frame=none,
numbers=none,
captionpos=none
}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\noindent\colorbox{gray}{\parbox{\linewidth}{#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white, font=scriptsize}
%=====================================================================================================
% Define background box capabilities for source code listings
% Taken from http://tex.stackexchange.com/questions/8851/how-can-i-highlight-some-lines-from-source-code?lq=1
%=====================================================================================================
\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% \btIfInRange{number}{range list}{TRUE}{FALSE}
%
% Test in int number <number> is element of a (comma separated) list of ranges
% (such as: {1,3-5,7,10-12,14}) and processes <TRUE> or <FALSE> respectively
\newcount\bt@rangea
\newcount\bt@rangeb
\newcommand\btIfInRange[2]{%
\global\let\bt@inrange\@secondoftwo%
\edef\bt@rangelist{#2}%
\foreach \range in \bt@rangelist {%
\afterassignment\bt@getrangeb%
\bt@rangea=0\range\relax%
\pgfmathtruncatemacro\result{ ( #1 >= \bt@rangea) && (#1 <= \bt@rangeb) }%
\ifnum\result=1\relax%
\breakforeach%
\global\let\bt@inrange\@firstoftwo%
\fi%
}%
\bt@inrange%
}
\newcommand\bt@getrangeb{%
\@ifnextchar\relax%
{\bt@rangeb=\bt@rangea}%
{\@getrangeb}%
}
\def\@getrangeb-#1\relax{%
\ifx\relax#1\relax%
\bt@rangeb=100000% \maxdimen is too large for pgfmath
\else%
\bt@rangeb=#1\relax%
\fi%
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% \btLstHL<overlay spec>{range list}
%
% TODO BUG: \btLstHL commands can not yet be accumulated if more than one overlay spec match.
%
\newcommand<>{\btLstHL}[1]{%
\only#2{\btIfInRange{\value{lstnumber}}{#1}{\color{orange!30}\def\lst@linebgrdcmd{\color@block}}{\def\lst@linebgrdcmd####1####2####3{}}}%
}%
\makeatother
%=====================================================================================================
%\AtBeginSection[]{ % Do nothing for \section*
% \setbeamercolor{section in toc}{fg=black}
% \setbeamercolor{subsection in toc}{fg=black}
% \begin{frame}<beamer>
% \frametitle{Outline \insertsectionhead}
% \tableofcontents[hidesubsections]
% \end{frame}
%}
\setbeamertemplate{footline}[frame number]
\begin{document}
\beamertemplatenavigationsymbolsempty
%=====================================================================================================
%=====================================================================================================
%=====================================================================================================
\begin{frame}
\titlepage
\begin{center}
\url{git@github.com:FelixPetriconi/accu2017_course.git}
\end{center}
\end{frame}
%=====================================================================================================
%=====================================================================================================
%=====================================================================================================
\begin{frame}{}
\begin{center}
The [C++] language is too large for \textit{anyone} to master\\
\pause
So \textit{everyone} lives within a subset\\
\pause
\end{center}
\begin{flushright}
{\small \textit{Sean Parent, C++Now, 2012}}
\end{flushright}
\end{frame}
%=====================================================================================================
%=====================================================================================================
%=====================================================================================================
\part{Why are were here?}
\begin{frame}{Felix Petriconi}
\begin{itemize}
\item School (UCSD Pascal, Turbo Pascal)
\item Studied electrical engineering (Modula 2, Ada, C++)
\item Student research assistant (1992-1996) (Turbo Pascal, C++, C)
\item Freelance programmer 1996-2003 (Ericsson, Siemens-VDO, etc.)
\begin{itemize}
\item Development of test software for embedded devices (Perl, C)
\end{itemize}
\item Programmer and development manager 2003-today at MeVis Medical Solutions AG, Bremen, Germany
\begin{itemize}
\item Development of medical devices in the area of mammography and radio therapy (C++, Ruby, Python)
\end{itemize}
\item Programming activities:
\begin{itemize}
\item Blog editor of ISO C++ website
\item Active member of C++ User Group Bremen
\item Contributor to Sean Parent's concurrency library
\item Member of ACCU conference committee
\end{itemize}
\item Married with Nicole, having three children, living near Bremen, Germany
\item Other interests: Classic film scores, composition
\end{itemize}
\end{frame}
\begin{frame}<handout:0>
\begin{center}
Why are we here?
\end{center}
\end{frame}
\section{Why am I here?}
\begin{frame}{Why am I here?}
\pause
I saw how we developed multi threaded code in the past.\\
I saw how easy it is to make mistakes.\\
I saw and still see how difficult it is to maintain this code.\\
\medskip \pause
I watched recordings from Sean Parent's talks about "Better Code".\\
I was impressed.\\
I wanted to learn more.\\
\medskip \pause
I'm collaborating in his open source project for a new library.\\
I'm continuously learning there a lot.\\
I care about sharing my knowledge, here at the ACCU conference.
\end{frame}
\section{Why are you here?}
\begin{frame}{Why are you here?}
\begin{center}
What is your motivation to be here?
\end{center}
\end{frame}
\section{Motivation}
\subsection{Problems from my domain}
\begin{frame}{Problems from my domain}
\begin{itemize}
\item Loading of huge images blocks UI
\item Storing of files blocks UI
\item Re-coding of huge images takes very long
\item DB accesses takes too long
\item ...
\end{itemize}
\end{frame}
\begin{frame}
\begin{center}
Why do we have to talk about concurrency?
\end{center}
\end{frame}
\begin{frame}{}
\begin{center}
The free lunch is over!
\end{center}
\pause
\begin{flushright}
\textit{Herb Sutter, 2005}\footnotemark
\end{flushright}
\pause
\only<3->{\footnotetext[1]{The Free Lunch Is Over: A Fundamental Turn Toward Concurrency in Software \url{http://www.gotw.ca/publications/concurrency-ddj.htm}} }
\end{frame}
\begin{frame}{The free lunch is over}
\includegraphics[width=0.8\textwidth]{Introduction/TheFreeLunchIsOver.jpg}
\end{frame}
\begin{frame}<handout:0>{Desktop Compute Power}
8-core 3.5GHz (Sandy Bridge + AMD Radeon 6950)
\includegraphics[width=\textwidth]{Introduction/DesktopComputePower01.jpg}
\end{frame}
\begin{frame}<handout:0>{Desktop Compute Power}
8-core 3.5GHz (Sandy Bridge + AMD Radeon 6950)
\includegraphics[width=\textwidth]{Introduction/DesktopComputePower02.jpg}
\end{frame}
\begin{frame}<handout:0>{Desktop Compute Power}
8-core 3.5GHz (Sandy Bridge + AMD Radeon 6950)
\includegraphics[width=\textwidth]{Introduction/DesktopComputePower03.jpg}
\end{frame}
\begin{frame}<handout:0>{Desktop Compute Power}
8-core 3.5GHz (Sandy Bridge + AMD Radeon 6950)
\includegraphics[width=\textwidth]{Introduction/DesktopComputePower04.jpg}
\end{frame}
\begin{frame}<handout:0>{Desktop Compute Power}
8-core 3.5GHz (Sandy Bridge + AMD Radeon 6950)
\includegraphics[width=\textwidth]{Introduction/DesktopComputePower05.jpg}
\end{frame}
\begin{frame}{Desktop Compute Power}
8-core 3.5GHz (Sandy Bridge + AMD Radeon 6950)
\includegraphics[width=\textwidth]{Introduction/DesktopComputePower06.jpg}
\linebreak
That's what we are targeting for!
\end{frame}
\begin{frame}<handout:0>{Amdahl's Law}
\begin{columns}
\begin{column}{0.25\textwidth}
$S(N) = \frac{1}{(1-P)+\frac{P}{N}}$\\
\begin{tabular}{ll}
$P:$ & {\small Synchronization $[0 - 1]$} \\
$N:$ & {\small Number of Cores}\\
\end{tabular}
\end{column}
\begin{column}{0.75\textwidth}
\end{column}
\end{columns}
\end{frame}
\begin{frame}{Amdahl's Law}
0\% Synchronization
\begin{columns}
\begin{column}{0.25\textwidth}
$S(N) = \frac{1}{(1-P)+\frac{P}{N}}$\\
$P = 0$
\end{column}
\begin{column}{0.75\textwidth}
\includegraphics[width=0.75\textwidth]{Introduction/AmdahlsLaw00.jpg}
\end{column}
\end{columns}
\end{frame}
\begin{frame}<handout:0>{Amdahl's Law}
10\% Synchronization
\begin{columns}
\begin{column}{0.25\textwidth}
$S(N) = \frac{1}{(1-P)+\frac{P}{N}}$\\
$P = 0.1$
\end{column}
\begin{column}{0.75\textwidth}
\includegraphics[width=0.75\textwidth]{Introduction/AmdahlsLaw10.jpg}
\end{column}
\end{columns}
\end{frame}
\begin{frame}<handout:0>{Amdahl's Law}
20\% Synchronization
\begin{columns}
\begin{column}{0.25\textwidth}
$S(N) = \frac{1}{(1-P)+\frac{P}{N}}$\\
$P = 0.2$
\end{column}
\begin{column}{0.75\textwidth}
\includegraphics[width=0.75\textwidth]{Introduction/AmdahlsLaw20.jpg}
\end{column}
\end{columns}
\end{frame}
\begin{frame}{Amdahl's Law}
90\% Synchronization
\begin{columns}
\begin{column}{0.25\textwidth}
$S(N) = \frac{1}{(1-P)+\frac{P}{N}}$\\
$P = 0.9$
\end{column}
\begin{column}{0.75\textwidth}
\includegraphics[width=0.75\textwidth]{Introduction/AmdahlsLaw90.jpg}
\end{column}
\end{columns}
\end{frame}
%=====================================================================================================
%=====================================================================================================
%=====================================================================================================
\part{Futures}
\begin{frame}{Outline Futures}
\tableofcontents
\end{frame}
\section{Futures}
\subsection{Why Futures?}
\begin{frame}{Why Futures?}
Why using futures?\\ \pause
Aren't threads, mutex, atomics great?\\ \pause
They are great tools "to shot yourself into the foot!"\\ \pause
It is so easy
\begin{itemize}
\item having race conditions
\item having dead locks
\item wasting CPU cycles through contention
\end{itemize}
\pause
Do you program your application in assembly? \\ \pause
Only if it absolute time critical.\\
Then don't use tools from the level of assembly!
\end{frame}
\subsection{Introduction}
\begin{frame}{Future Introduction}
\begin{center}
\includegraphics[width=0.6\textwidth]{Futures/future.jpg}
\end{center}
\begin{itemize}
\item<1->Futures provide a mechanism to separate a function from its result
\item<2->After the function is called the result appears "magically" in the future
\item<3->A future is a token to the result of a function
\item<4->Added with C++11
\item<5->Futures, resp. promises where invented 1977/1978 by Daniel P. Friedman, David Wise, Henry Baker and Carl Hewitt
\end{itemize}
\end{frame}
\subsection{C++ Standard - Futures}
\begin{frame}{C++ Standard - Futures}
\lstinputlisting[language=C++,
linebackgroundcolor={%
\btLstHL<2>{1}%
\btLstHL<3>{8-11}%
\btLstHL<4>{13}%
\btLstHL<5>{15}%
\btLstHL<6>{16}%
}
]{Futures/future01.cpp}
\pause\pause\pause\pause\pause
\outputbox{42}
\end{frame}
\subsubsection{Exceptions}
\begin{frame}{C++ Standard - Futures - Exceptions}
\lstinputlisting[language=C++,
linebackgroundcolor={%
\btLstHL<2>{8-11}%
\btLstHL<3>{13}%
\btLstHL<4>{16,20}%
\btLstHL<5>{17-18}%
\btLstHL<6>{20-22}%
}
]{Futures/future02.cpp}
\pause\pause\pause\pause
\outputbox{Bad things happened: Vogons appeared!}
\end{frame}
\subsubsection{Deficiencies}
\begin{frame}{C++11/14 Future Deficiencies}
\begin{itemize}
\item<2->No continuation –-- .then() \xmark $^\ast$
\item<3->No join –-- .when\_all() and .when\_any() \xmark $^\ast$
\item<4->No split –-- continuation in different directions \xmark
\item<5->No cancellation (but can be modelled) \xmark
\item<6->No progress monitoring (except ready) \xmark
\item<7->No custom executor \xmark
\item<8->Blocks on destruction (may even blocks until termination of used thread) \xmark
\item<9->.get() has two problems:
\begin{enumerate}
\item<10->One thread resource is consumed which increases contention and possibly causing a deadlock \xmark
\item<11->Any subsequent non-dependent calculations on the task are also blocked \xmark
\end{enumerate}
\item<12->Don't behave as a regular type \xmark
\end{itemize}
\bigskip
\pause$\ast$ Comes with C++17(TS)
\end{frame}
\subsection{Boost - Futures}
\subsubsection{Deficiencies}
\begin{frame}{Boost - Futures}
\begin{itemize}
\item<2->Continuation –-- .then() \cmark
\item<2->Join –-- .when\_all() and .when\_any() \cmark
\item<3->No split –-- continuation in different directions \xmark
\item<3->No cancellation (but can be modelled) \xmark
\item<3->No progress monitoring (except ready) \xmark
\item<4->Custom executor \cmark
\item<5->Blocks on destruction (may even blocks until termination of used thread) \xmark
\item<5->.get() has two problems:
\begin{enumerate}
\item<5->One thread resource is consumed which increases contention and possibly causing a deadlock \xmark
\item<5->Any subsequent non-dependent calculations on the task are also blocked \xmark
\end{enumerate}
\item<5->Don't behave as a regular type \xmark
\end{itemize}
\end{frame}
\subsubsection{Future Continuation}
\begin{frame}{Future Continuation}
\includegraphics[width=\textwidth]{Futures/continuation.jpg}
\end{frame}
\begin{frame}{C++17(TS) / Boost - Continuation}
\lstinputlisting[language=C++,
linebackgroundcolor={%
\btLstHL<2>{7}%
\btLstHL<3>{9-10}%
\btLstHL<4>{13}%
}
]{Futures/future03.cpp}
\pause\pause\pause
\outputbox{42}
\end{frame}
\subsubsection{Future Join}
\begin{frame}{Future Join}
\includegraphics[width=\textwidth]{Futures/joins.jpg}
\end{frame}
\begin{frame}{C++17(TS) / Boost - Join}
\lstinputlisting[language=C++,
linebackgroundcolor={%
\btLstHL<2>{7}%
\btLstHL<3>{8}%
\btLstHL<4>{10}%
\btLstHL<5>{11-14}%
\btLstHL<6>{17}%
}
]{Futures/future04.cpp}
\pause\pause\pause\pause\pause
\outputbox{42}
\end{frame}
\begin{frame}{C++17(TS) / Boost - Join}
\lstinputlisting[language=C++,
linebackgroundcolor={%
\btLstHL<1->{11}%
}
]{Futures/future04.cpp}
What is the type of f?\linebreak
\pause
f is a future tuple of futures: \lstinline[style=CodeInTextStyle]|future<tuple<future<int>, future<int>>>|
\end{frame}
\subsection{stlab - Futures}
\begin{frame}{stlab - Futures}
\includegraphics[width=0.25\textwidth]{Futures/stlab_logo.png}
\begin{center}
\textbf{stlab::future}\\
Source: \url{https://github.com/stlab/libraries}\\
Documentation: \url{http://www.stlab.cc/libraries}
\end{center}
\end{frame}
\begin{frame}{stlab - Futures}
\begin{itemize}
\item<2->Continuation –-- .then() \cmark
\item<3->Join –-- .when\_all() and .when\_any() \cmark
\item<4->Split –-- continuation in different directions \cmark
\item<5->Cancellation \cmark
\item<6->No progress monitoring (except ready), more planned \xmark
\item<7->Custom executor \cmark
\item<8->Do not block on destruction \cmark
\item<9->Behave as a regular type \cmark
\item<10->Additional dependencies:
\begin{itemize}
\item C++14: boost (optional, variant)
\item C++17: none
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}{stlab::future}
\lstinputlisting[language=C++,
linebackgroundcolor={%
\btLstHL<2>{1-2}%
\btLstHL<3>{6-9}%
\btLstHL<4>{11-14}%
\btLstHL<5>{15}%
\btLstHL<6>{10}%
\btLstHL<7>{18-20}%
\btLstHL<8>{22-23}%
}
]{Futures/future11.cpp}
\pause\pause\pause\pause\pause\pause\pause
\outputbox{42}
\end{frame}
\begin{frame}{stlab::future - Exceptions}
\lstinputlisting[language=C++,
linebackgroundcolor={%
\btLstHL<2>{7-10}%
\btLstHL<3>{11}%
\btLstHL<4>{14-15,19}%
\btLstHL<5>{21-23}%
}
]{Futures/future12.cpp}
\pause\pause\pause\pause
\outputbox{Bad things happened: Vogons appeared!}
\end{frame}
\begin{frame}{stlab::future - Continuation}
\lstinputlisting[language=C++,
linebackgroundcolor={%
\btLstHL<2>{6-7}%
\btLstHL<3>{9}%
\btLstHL<4>{11-13}%
\btLstHL<5>{10}%
\btLstHL<6>{11}%
}
]{Futures/future13.cpp}
\pause\pause\pause\pause
\outputbox{42}
\end{frame}
\subsubsection{Executors}
\begin{frame}{Executors}
\begin{itemize}
\item<2-> Executors are needed to customize where the task shall be executed
\item<3-> Executors can be general thread pools, serial queues, main queues, dedicated task groups, etc.
\end{itemize}
\end{frame}
\begin{frame}{stlab::future - Continuation with Custom Executor}
\lstinputlisting[language=C++,
linebackgroundcolor={%
\btLstHL<2>{10-11}%
\btLstHL<3>{13-14}%
\btLstHL<4>{15}%
}
]{Futures/future14.cpp}
\end{frame}
\begin{frame}{stlab::future - Custom Executor}
\begin{itemize}
\item<2-> In boost, executors derive from a common base class
\item<3-> In stlab the executors must provide \lstinline[style=CodeInTextStyle]|
template <typename F>
void operator()(F f)|
\item<4-> Let's build exemplary a custom executor for the Qt GUI, that allows to perform updates in the Qt main event loop
\end{itemize}
\end{frame}
\begin{frame}{stlab::future - Custom Executor - Qt}
\lstinputlisting[language=C++,
linerange={1-7,9-11,26-27,40-47},
linebackgroundcolor={%
\btLstHL<2>{4}%
\btLstHL<3>{6}%
\btLstHL<4>{13-14}%
\btLstHL<5>{15-16}%
\btLstHL<6>{8-10}%
}
]{Futures/QtScheduler.cpp}
\end{frame}
\begin{frame}{stlab::future - Custom Executor - Qt cont. I}
\lstinputlisting[language=C++,
linerange={1-27,40-41,47-47},
linebackgroundcolor={%
\btLstHL<1>{10}%
\btLstHL<2>{16-17}%
\btLstHL<3>{12,18}%
\btLstHL<4>{8,13,19}%
\btLstHL<5>{20}%
\btLstHL<6>{23}%
\btLstHL<7>{25}%
}
]{Futures/QtScheduler.cpp}
\end{frame}
\begin{frame}{stlab::future - Custom Executor - Qt cont. II}
\lstinputlisting[language=C++,
linerange={1-5,10-11,25-39,40-47},
linebackgroundcolor={%
\btLstHL<1>{8}%
\btLstHL<2>{11}%
\btLstHL<3>{14}%
\btLstHL<4>{15,18}%
\btLstHL<5>{16,18,20}%
\btLstHL<6>{17, 26}%
}
]{Futures/QtScheduler.cpp}
\end{frame}
\subsubsection{Error Recovery}
\begin{frame}{stlab::future - Error Recovery}
\lstinputlisting[language=C++,
linerange={5-26},
linebackgroundcolor={%
\btLstHL<2>{2-5}%
\btLstHL<3>{6-9}%
\btLstHL<4>{11}%
\btLstHL<5>{12}%
\btLstHL<6>{13-16}%
\btLstHL<7>{17}%
\btLstHL<8>{18}%
}
]{Futures/future15.cpp}
\pause\pause\pause\pause\pause\pause\pause\pause
\outputbox{
Listen to Vogon poetry!\\
We have a problem!\\}
\end{frame}
\subsubsection{Join}
\begin{frame}{stlab::future - Join}
\lstinputlisting[language=C++,
linebackgroundcolor={%
\btLstHL<2>{8}%
\btLstHL<3>{9}%
\btLstHL<4>{11}%
\btLstHL<5>{12}%
\btLstHL<6>{13}%
\btLstHL<7>{8,9,14}%
\btLstHL<8>{16-18}%
\btLstHL<9>{19}%
}
]{Futures/future16.cpp}
\pause\pause\pause\pause\pause\pause\pause\pause
\outputbox{42}
\end{frame}
\subsubsection{Splits}
\begin{frame}{future - Split}
\includegraphics[width=\textwidth]{Futures/split.jpg}
\end{frame}
\begin{frame}{stlab::future - Split}
\lstinputlisting[language=C++,
linebackgroundcolor={%
\btLstHL<2>{8}%
\btLstHL<3>{10-12}%
\btLstHL<4>{14-16}%
\btLstHL<5>{18-20}%
}
]{Futures/future17.cpp}
\pause\pause\pause\pause
\outputbox{
Tell the answer May the answer 42 Arthur Dent\\
42 shear up Marvin \pause $\textcolor{red}{\Rrightarrow}$ \textbf{\textcolor{red}{Race condition by using std::cout}}
}
\end{frame}
\subsection{Exercise 1}
\begin{frame}<handout:0>{Exercise 1}
\begin{center}
Demo Exercise 1
\end{center}
\end{frame}
\begin{frame}{Exercise 1}
Change the application in a way that
\begin{itemize}
\item using Start does not block the UI,
\item it is possible to cancel the running operation,
\item it is possible to restart it.
\end{itemize}
\end{frame}
\begin{frame}{Conclusion}
Futures are a great concept to structure the code so that it runs with minimal contention.\\
\pause
\medskip
After a single execution the graph cannot be used any more.
\end{frame}
%=====================================================================================================
%=====================================================================================================
%=====================================================================================================
\part{Channels}
\begin{frame}{Outline Channels}
\tableofcontents
\end{frame}
\section{Channel Motivation}
\begin{frame}{Channel Introduction}
\begin{center}
\includegraphics[width=0.5\textwidth]{Channels/simple_channel.png}
\end{center}
\begin{itemize}
\item <2->Each change triggers a notification to the sink values
\item <3->Channels allow the creation of persistent execution graphs
\item <4-> This is also known as reactive programming model
\item <5-> First published by Tony Hoare 1978
\end{itemize}
\end{frame}
\section{Channel - Stateless Process}
\begin{frame}{Channel - Stateless Process}
\lstinputlisting[language=C++,
linebackgroundcolor={%
\btLstHL<2>{1}%
\btLstHL<3>{2}%
\btLstHL<4>{5}%
\btLstHL<5>{6}%
\btLstHL<6>{7-8}%
\btLstHL<7>{10-11}%
\btLstHL<8>{13-15}%
\btLstHL<9>{16}%
\btLstHL<10>{18}%
\btLstHL<11>{20}%
}
]{Channels/channel01.cpp}
\pause\pause\pause\pause\pause\pause\pause\pause\pause\pause\pause
\outputbox{
1\\
2\\
3\\}
\end{frame}
\subsection{Channel - Split}
\begin{frame}{Channel - Split}
\begin{center}
\includegraphics[width=0.75\textwidth]{Channels/simple_split.png}
\end{center}
\end{frame}
\begin{frame}{Channel - Split Process}
\lstinputlisting[language=C++,
linerange={5-21},
linebackgroundcolor={%
\btLstHL<2>{3-5}%
\btLstHL<3>{7}%
\btLstHL<4>{8}%
\btLstHL<5>{10}%
\btLstHL<6>{11}%
\btLstHL<7>{13}%
\btLstHL<8>{15}%
}
]{Channels/channel02.cpp}
\pause\pause\pause\pause\pause\pause\pause\pause
\outputbox{
Process A 1 \\
Process B 1 \\
Process A 2 \\
Process B 2 \\
Process B 3 \\
Process A 3 \\
}
\end{frame}
\subsection{Channel - Join}
\begin{frame}{Channel - Join}
\includegraphics[width=\textwidth]{Channels/join.png}
\end{frame}
\begin{frame}{Channel - Joined Processes}
\lstinputlisting[language=C++,
linerange={6-25},
linebackgroundcolor={%
\btLstHL<2>{6-7}%
\btLstHL<3>{9}%
\btLstHL<4>{11-12}%
\btLstHL<5>{14-15}%
\btLstHL<6>{17}%
}
]{Channels/channel03.cpp}
\pause\pause\pause\pause\pause\pause
\outputbox{
Process 1 3 \\
Process 2 5 \\
Process 4 6 \\
}
\end{frame}
\begin{frame}{Channel operators}
Beside \lstinline[style=CodeInTextStyle]|join()| there are:\\
\begin{itemize}
\item \lstinline[style=CodeInTextStyle]|zip()|The process takes the passed values in a round-robin manner, starting with the result from the first receiver.
\item \lstinline[style=CodeInTextStyle]|merge()|The process takes the values in an arbitrary order.
\end{itemize}
\end{frame}
\subsection{Exercise 2}
\begin{frame}<handout:0>{Exercise 2}
\begin{center}
Demo Exercise 2
\end{center}
\end{frame}
\begin{frame}{Exercise 2}
Create a process chain with
\begin{itemize}
\item the inputs
\begin{itemize}
\item one \lstinline[style=CodeInTextStyle]|int| input
\item one \lstinline[style=CodeInTextStyle]|std::string| input
\item one \lstinline[style=CodeInTextStyle]|double| input
\end{itemize}
\item all inputs are joined to a process that concatenates all the results into a string and
\item the result is split into