-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconcurrent-cplusplus.html
More file actions
1200 lines (964 loc) · 35.8 KB
/
concurrent-cplusplus.html
File metadata and controls
1200 lines (964 loc) · 35.8 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
<!DOCTYPE html>
<html>
<head>
<title>Concurrency in C++</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<!-- when changing the stylesheet file please see also remark below -->
<link rel="stylesheet" type="text/css" href="styling.css" />
</head>
<body>
<!-- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -->
<!-- :::::::::: template pages come first ... skip to REALCONTENT ::::::::: -->
<!-- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -->
<textarea id="source">
layout: true
name: blank
styling: styling.css
styling-by: Martin Weitzel
<!--
*****************************************************************************
Template used for title page (only)
*****************************************************************************
Please change the 'styling-by:' attribute if you change the style-sheet.
-->
.stylehint[
Styled with [{{styling}}]({{styling}}) by {{styling-by}}
]
---
layout: true
name: plain
copyright: (CC) BY-SA
branding: [Dipl.-Ing. Martin Weitzel](http://tbfe.de)
customer: [im Auftrag von MicroConsult Training & Consulting GmbH](http://microconsult.de)
<!--
*****************************************************************************
Template used for for pages NOT referring to any Info-Graphic
*****************************************************************************
The following attributes are mandatory FOR THE TEMPLATE PAGE and should
simply be left empty if not meaningful.
copyright: will be reproduced in each page footer first
branding: will reproduced in each page footer next
customer: will be reproduced in each page footer last
As the above attributes are part of several page templates a global replace
should be used for consistent changes.
On pages USING THIS TEMPLATE the following attributes must be set:
header: ## and header text (i.e. including the markdown formatting indicator)
-->
{{header}}
.pagefooter[
{{copyright}}: {{branding}} {{customer}} .microconsult-logo[]
]
---
template: blank
# Agenda: Modern C++
Focussing
* Concurrency in C++
© 2019: [Creative Commons BY-SA]
* by: [Dipl.-Ing. Martin Weitzel](http://tbfe.de)
* for: [MicroConsult GmbH Munich](http://microconsult.com)
[Creative Commons BY-SA]: https://creativecommons.org/licenses/by-sa/4.0/
---
template: plain
name: cpp11_concurrency_basics
header: ## C++11: Concurrency Basics
With C++11 support for multi-threading was introduced.._[]
---------------------------------------------------------------------------
* [Parallelizing Independent Tasks ](#parallelizing_tasks)
* [Synchronisation with Mutexes ](#mutex_synchronisation)
* [One-Time Execution ](#onetime_execution)
* [Messaging with Condition Variables ](#condition_variables)
* [Atomic Operations ](#atomic_operations)
* [Direct Use of Threads ](#direct_use_of_threads)
* [Native Threading Model Handles ](#native_thread_handles)
* [Concurrency Recommendations ](#concurrency_recommendations)
---------------------------------------------------------------------------
This part of the presentation was written with the intent to give an overview
of the provided features only, it is by far not exhaustive!
.F[:
Concurrency features in C++11 are more as "just some more library classes and
functions". Especially with regard to optimisations and how to provide
[Cache Coherence] on modern CPUs it is closely intertwined with code
generation. For more information see:
http://en.cppreference.com/w/cpp/language/memory_model and
http://en.cppreference.com/w/cpp/language/memory_order
]
[Cache Coherence]: http://en.wikipedia.org/wiki/Cache_coherence
---
template: plain
name: parallelizing_tasks
header: ### Parallelizing Independent Tasks
For complex tasks that can be split into independent parts, concurrency **and
scalability to multiple cores** can be easily achieved by following a simple
recipe:
1. Separate the task into a number of different functions (or calls of the same
function with different arguments).
2. Run each such function by handing it over to `std::async`, storing the
`future` that is returned (easiest in an `auto`-typed variable).
3. Fetch (and combine) the results by calling the member function `get` for
each `future`.
That way all the functions may run concurrently and the last step synchronizes
by waiting for completion.
.I[
For more information on parallelizing independent task that way see:
http://en.cppreference.com/w/cpp/thread/async
]
.F[:
Technically the return value of `std::async` is an `std::future` but as this
is a template and the type is usually somewhat different to spell out, most
usages of `std::async` store the result in an `auto`-typed variable.
]
---
template: plain
name: futures_and_promises
header: #### Foundation: Futures and Promises
The foundation on which parallelizing tasks is build are [Futures and Promises].
These need not be fully understood to apply the API (exemplified on the next
pages), but may help to understand the basic machinery:
* A future is the concrete handle which a client can use to fetch the result,
presumably made available by a different thread of execution.
* A promise is a helper class which may be used in a separate thread to make
a result available for a client.
.I[
For more information on promises and futures see:
http://en.cppreference.com/w/cpp/thread/future
http://en.cppreference.com/w/cpp/thread/promise
http://en.cppreference.com/w/cpp/thread/packaged_task
]
[Futures and Promises]: http://en.wikipedia.org/wiki/Futures_and_promises
---
template: plain
name: parallelizing_example
header: #### Parallelizing Example
The following example calculates the sum of the first `N` elements in `data`
by splitting the work of `std::accumulate`, into two separate function calls,
that may run concurrently:._[]
```
// calculate sum of first N values in data
//
long long sum(const int data[], std::size_t N) {
auto lower_part_sum = std::async(
[=]{ return std::accumulate(&data[0], &data[N/2], 0LL); }
);
auto upper_part_sum = std::async(
[=]{ return std::accumulate(&data[N/2], &data[N], 0LL); }
);
return lower_part_sum.get()
+ upper_part_sum.get();
}
```
.F[:
Note the use of lambdas above – the actual call to `std::accumulate` will only
happen when the lambda gets executed! A similar effect can be achieved as
follows:
```
// preparing the callable for std::async with std::bind:
… std::async(std::bind(std::accumulate, &data[0], &data[N/2], 0LL)) …
… std::async(std::bind(std::accumulate, &data[N/2], &data[N], 0LL)) …
```
]
---
template: plain
header: #### Default Launch Policy
The default behavior is that the systems decides on its own whether an
asynchronously started task is run concurrently.._[]
Using `std::async` **without an explicit launch policy**
* **is just a hint** that it is acceptable to run a callable unit of code
concurrently,
* as long as it has finished (and possibly returned a result) latest when
the `get`-call returns, which has bee invoked on the `std::future`.
.W[
Be sure **not** to use the default launch policy but specify concurrent
execution explicitly (see next page) whenever it is essential that two
callable run concurrently.
]
.F[:
It is a well known effect that too many parallel threads of execution may rather
degrade performance. Especially if threads are CPU bound, it makes little sense
to have more threads as cores. Therefore the standard gives considerable freedom
to the implementation, which might implement concurrency with `std::async` with
a thread pool and turn to synchronous execution or lazy evaluation at a certain
threshold.
]
---
template: plain
header: #### Explicit Launch Policies
There is a second version of `std::async` which has a first argument to specify
the launch strategy.
The standard defines two values:
* `std::launch::async`
if **not set** the callable will **not** run on its own thread;._[]
* `std::launch::deferred`
of **set** the callable will **not** be called before `get` is invoked.
.I[
For more information on launch policies see:
http://en.cppreference.com/w/cpp/thread/launch
]
.F[:
One case in which this setting this flag may make sense is to test the program
logic independent from possible problems created through race conditions or
deadlocks, originating from dependencies between the "independent tasks", that
had been overlooked.
]
---
template: plain
header: #### Catching Exceptions
If the callable started via `std::async` throws an exception, it will appear as
if it were thrown from the call to get.
Hence, if the asynchronously run task may throw, fetching the result should be
done in a try block:
```
auto task1 = std::async( … ); // whatever-is-to-do (and may throw)
auto task2 = std::async( … ); // whatelse-is-to-do (and may throw)
…
try { … task1.get() … }
} catch ( … ) { // what may be thrown from whatever-is-to-do
… // handle the case that whatever-is-to-do threw
}
try { … task2.get() … }
} catch ( … ) { // what may be thrown from whatelse-is-to-do
… // handle the case that whatelse-is-to-do threw
}
```
---
template: plain
header: #### Communication between Independent Tasks
First of all: If the need arises to communicate between independent tasks, this
should be taken **as a strong warning** that such tasks are actually not
independent.
.W[
If parallel tasks are not independent, further needs follow quickly with respect
to synchronize access to shared data … **with all the further intricacies following
from this**.._[]
]
Nevertheless there is one common case that requires a simple form of
communication between otherwise independent tasks.
.N[
* If there are several tasks working towards a common goal
* of which one fails, making the goal unattainable,
* the others should not waste CPU-time needlessly.
]
.F[:
In other words: Pandora's proverbial can of worms opens quickly and widely …
]
---
template: plain
header: #### Communicate Failure between Concurrent Tasks
A basic design that communicates failure between partners working towards a
common goal is outlined in the following example.._[]
.pull-left[
The workers could look about so …
```
void foo ( … , bool &die) {
…
for ( … ) {
if (die) return;
…
… // some complex
… // algorithm
…
// may fail here
if ( … ) {
die = true;
return;
}
}
}
```
]
.pull-right[
… being run by that code:
```
bool die = false;
auto task1 = std::async(
[&die]{ foo( … , die); }
);
auto task2 = std::async(
[&die]{ foo( … , die); }
);
…
… task1.get() …
… task2.get() …
…
if (die) {
// goal not reached
…
}
```
]
.F[:
To keep this code simple it just returns in case of problems, though it requires
only a few changes if problems should be communicated to the caller via exceptions.
]
---
template: plain
name: mutex_synchronisation
header: ### Synchronisation with Mutexes
The word *Mutex* abbreviates *Mutual Exclusion* and describe the basic purpose
of the feature.
* Allow only one thread to enter a [Critical Section], typically non-atomically
executed sequence of statements
* which temporarily invalidate an [Class Invariant], or
* in other ways accesses a resource not designed for shared use.
.N[
In general, mutexes have at least two operations._[] for
* **lock**-ing and
* **unlock**-ing,
but frequently provide additional features to make their practical use more
convenient or less error prone.
]
[Critical Section]: http://en.wikipedia.org/wiki/Critical_section
[Class Invariant]: http://en.wikipedia.org/wiki/Class_invariant
.F[:
Though the operations may not be spelled exactly *lock* and *unlock* …
(especially as mutexes are somewhat related to [Semaphores], which originally
named their lock (-like) acquire operation *P* and their unlock (-like) release
operation *V*.
]
---
template: plain
name: mutex_example_1
header: #### Mutex Example (1)
The following example calculates one table from another one:._[]
```
template<typename In, typename Out, typename Transformation>
void worker(const In data[], std::size_t data_size,
Out result[], std::size_t &total_progress,
Transformation func) {
static std::mutex critical_section;
while (total_progress < data_size) {
critical_section.lock();
constexpr auto chunks = std::size_t{100};
const auto beg = total_progress;
const auto end = ((data_size - total_progress) > chunks)
? total_progress += chunks
: total_progress = data_size;
critical_section.unlock();
std::transform(&data[beg], &data[end], &result[beg], func);
}
}
```
.F[:
The work is shared by any number of `worker` task run concurrently, each
fetches and transforms a fixed number of values. This can be advantageous to
splitting the work by calculating fixed-size regions of the table in advance, if
the transformation function has a largely varying runtime depending on the
argument value.
]
---
template: plain
name: mutex_example_2
header: #### Mutex Example (2)
Assuming the transformation is to calculate square roots and there are
two arrays of size `N`, say
* `data` (filled with values to transform), and
* `sqrts` to store the results
workers may be created (to be handed over to `std::async`) as follows:._[]
```
std::size_t processed_count = 0;
auto worker_task =
[&]() { worker(data, N, sqrts, processed_count,
[](double e) { return std::sqrt(e); });
};
```
.F[:
Given the above, a particular nifty way to create and run workers were:
```
// assuming NCORES holds the number of cores to use by workers:
std::array<std::future<void>, NCORES> workers;
for (auto &w : workers)
w = std::async(worker_task);
for (auto &w : workers)
try { w.get(); } catch (...) {}
```
]
---
template: plain
name: mutexes_and_raii
header: #### Mutexes and RAII
As the mutex operations **lock** and **unlock** need to come correctly paired,
they make a good candidate to apply a technique called [RAII].._[]
It works by creating a wrapper class, executing
* the acquiring operation (or *lock*-ing in this case) in its constructor, and
* the releasing operation (or *unlock*-ing) in its destructor.
Such helper classes are available in C++11 `std::lock_guard`.
The big advantage is that unlocking the mutex is guaranteed for code blocks
defining a RAII-style (guard) object locally, no matter whether control
flow reached its end, or by `break`, `return`, or some exception.
.I[
For more information on the RAII-style use of mutexes see:
http://en.cppreference.com/w/cpp/thread/lock_guard
]
.F[:
This [TLA] is an abbreviation Bjarne Stroustrup once coined for *Resource
Acquisition is Initialisation*. In a recent interview Stroustrup revealed that he
is not particularly happy with the term he once choose. But to change it would
require to travel back in a time machine and suggest something more appropriate
to him, as today the term RAII is in much too widespread use to be replaced
by something else.
]
[RAII]: http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization
[TLA]: http://www.catb.org/jargon/html/T/TLA.html
---
template: plain
name: mutexes_and_raii
header: #### Mutex Variants
Mutexes in C++11 come in a number of flavours, which controlling their behaviour
with respect to the following details:
* Whether or not some thread that already locked a mutex may lock it once more
(and needs to release it as often).
* Whether or not a thread waits if it finds a mutex locked by some other thread,
and in the latter case until *when* (clock-based time point) or or *how long*
(duration) it waits.
For all mutex variants there are also variants of RAII-style lock guards.
.I[
For more information on the different variants of mutexes and RAII-style
wrappers see:
http://en.cppreference.com/w/cpp/thread/recursive_mutex
http://en.cppreference.com/w/cpp/thread/timed_mutex
http://en.cppreference.com/w/cpp/thread/recursive_timed_mutex and
http://en.cppreference.com/w/cpp/thread/unique_lock
]
---
template: plain
name: mutexes_and_raii
header: #### C++14: Upgradable Locks
C++14 added the class `std::shared_lock`, supporting a frequent necessity:._[]
.N.center[
[Multiple Reader/Single Write Locking Schemes](http://en.wikipedia.org/wiki/Readers%E2%80%93writer_lock)
]
* Any number of (reader) threads may successfully `shared_lock` that kind of
mutex …
* … but only one single (writer) thread is allowed to actually `lock` it
(unshared).
C++14 also provides a RAII-style wrapper for shared locking.
.I[
For more information on shared locking see:
http://en.cppreference.com/w/cpp/thread/shared_timed_mutex and
http://en.cppreference.com/w/cpp/thread/shared_lock
]
.F[:
Note that the terms "reader" and "writer" indicates the typical use of that kind
of mutex, assuming that it is sufficient for readers to obtain the lock shared,
as it guarantees the invariants hold but no modifications are made, while writers
will need to temporarily break invariants.
]
---
template: plain
name: mutexes_and_raii
header: #### Defeating Deadlocks Caused by Mutex-Locking
As a potentially blocking mechanism mutexes are famous for creating deadlocks, i.e
* in the situation where two resources *A* and *B* are required,
* one thread acquires these in the order *first A, then B* and
* another thread acquires these in the order *first B, then A*.._[]
The obvious counter measure is to acquire locks always in the same order,
as achievable with `std::lock` and `std::try_lock`.
.I[
For more information on locking several mutexes semantically atomic (i.e.
without creating the potential for dead-locks) see:
http://en.cppreference.com/w/cpp/thread/lock and
http://en.cppreference.com/w/cpp/thread/try_lock
]
.F[:
In practice, the potential for deadlocks is often not as obvious as in this
example but much more intricate and close to impossible to spot, even in
scrutinising code reviews. So, if (accidental) deadlocks cannot be avoided,
sometimes a "self-healing" strategy is applied that works follows:
**If more than one lock needs to be acquired, acquire at least all others with
setting a time-out.** If that hits, **release all locks** acquired so far,
**delay** for some small amount of time (usually determined in a window with
some slight randomness), then **try again** (and maybe in a different order).
]
---
template: plain
name: onetime_execution
header: ### One-Time Execution
For a particular scenario that would otherwise require the use of mutex-es to
avoid a [Race Condition], there is pre-built solution in C++11.
Executing a piece of code exactly once can be achieved in a cookbook-style as
follows:
```
// in a scope reachable from all usage points:
std::once_flag this_code_once;
…
std::call_once(this_code_once, …some callable… ); // somewhere
…
std::call_once(this_code_once, … ); // maybe somewhere else
```
For any of the callables associated with the same instance of an
`std::once_flag` via `std::call_once`, without further protection via mutexes
it is guaranteed that **at most one** is executed **at most once**.
.I[
For more information on guaranteed one time execution see:
http://en.cppreference.com/w/cpp/thread/once_flag and
http://en.cppreference.com/w/cpp/thread/call_once
]
[Race Condition]: http://en.wikipedia.org/wiki/Race_condition
---
template: plain
name: onetime_execution
header: #### One-Time Execution Example
A typical use case for guaranteed one-time execution is some initialisation,
that may be expensive and is therefore delayed until a function that depends
on it is called the first time.
The following fragment avoids parallel initialisations of `table`:
```
… foo( … ) {
static std::once_flag init;
static std::array<int, 1000> table;
std::call_once(init, [&table]{
… // precalculate table when foo runs for the first time
});
… //
}
```
---
template: plain
name: onetime_execution
header: #### Local `static` Initialisation
Since C++11 supports multi-threading in the core language, initialising local
`static` variables is protected to be executed at most once:
```
… foo( … ) {
static const int z = expensive_calculation();
… //
}
```
As since C++11 compilers are required to wrap the necessary protection around
the initialisation of static locals, also this is guaranteed to work:._[]
```
class Singleton {
… //
public:
static Singleton &getInstance() {
static Singleton instance;
return instance;
}
};
```
.F[:
Non-believers should consider to copy the above code, paste it to
https://gcc.godbolt.org/ (or similar) after adding a member with a
runtime-dependant initialisation, and view the assembler output …
]
---
template: plain
name: condition_variables
header: ### Notifications with Condition Variables
A well-known abstraction in concurrent programming are
combining mutexes with a signalling mechanism.
One main use of condition variables is to **avoid busy waiting** in
producer-consumer designs,
* where consumer and producer run concurrently,
* exchanging data over some buffer data structure.
.I[
For more information on condition variables see:
http://en.cppreference.com/w/cpp/thread/condition_variable
]
---
template: plain
name: condition_variables
header: #### Condition Variable Example (1)
The following *RingBuffer* class can put condition variables to good use …
```
template<typename T, std::size_t N>
class RingBuffer {
std::array<T, N+1> buf;
std::size_t p = 0, g = 0;
bool empty() const { return p == g; }
bool full() const { return (p+1) % buf.size() == g; }
public:
void put(const T &val) {
if (full())
… // handle case no space is available
buf[p++] = val; p %= buf.size();
}
void get(T &val) {
if (empty())
… // handle case no data is available
val = buf[g++]; g %= buf.size();
}
};
```
… exactly at the currently omitted points.
---
template: plain
name: condition_variables
header: #### Condition Variable Example (2)
Obviously there are two conditions, that need special attention:
* The buffer may be full when `put` is called, or
* it may be empty, when `get` is called.
Therefore two condition variables._[] are added, furthermore a mutex to protect
accessing the buffer:
```
class RingBuffer {
… //
… // as before
… //
std::condition_variable data_available;
std::condition_variable space_available;
std::mutex buffer_access;
public:
… // see next page
};
```
.F[:
As the buffer space cannot be full and empty at the same time, technically
one condition variable would suffice, but for this introductory example using
two different instances seems to be clearer.
]
---
template: plain
name: condition_variables
header: #### Condition Variable Example (3)
There are two operations (of interest here), applicable to condition variables,
**sending** and **waiting for** notifications:._[]
```
class RingBuffer
… // see previous page
public:
void put(const T &val) {
std::unique_lock<std::mutex> lock(buffer_access);
space_available.wait(lock, [this]{ return !full(); });
buf[p++] = val; p %= buf.size();
data_available.notify_one();
}
void get(T &val) {
std::unique_lock<std::mutex> lock(buffer_access);
data_available.wait(lock, [this]{ return !empty(); });
val = buf[g++]; g %= buf.size();
space_available.notify_one();
}
};
```
.F[:
Essential here is also the connection between the condition variables, the mutex
protecting the `RingBuffer` invariants, and the conditions checked as part of
waiting, which are detailed on the next page.
]
---
template: plain
name: waiting_in_details
header: #### Waiting Anatomy
Waiting on a condition variable – as shown on the last page – with
```
// as part of put:
std::unique_lock<std::mutex> lock(buffer_access);
space_available.wait(lock, [this]{ return !full(); });
```
```
// as part of get:
std::unique_lock<std::mutex> lock(buffer_access);
data_available.wait(lock, [this]{ return !empty(); });
```
is equivalent to the following, **with the mutex being locked before**:
.pull-left[
```
// as part of put:
while (full()) { // !!full()
buffer_access.unlock();
// wait for notification
buffer_access.lock();
}
```
]
.pull-right[
```
// as part of get:
while (empty()) { // !!empty
buffer_access.unlock();
// wait for notification
buffer_access.lock();
}
```
]
At this point **the mutex is locked** (again) and **the condition is true.**
---
template: plain
name: spurious_wakeups
header: #### Spurious Wakeups
In the example code before, the loop (in the equivalent of `wait`-ing) may seem
unnecessary, as the respective notification will be sent only after some action
has made the condition true.
Nevertheless it makes sense and may even be necessary:
* **First of all, an implementation is allowed to give [Spurious Wake-Up]s,
so the loop is necessary anyway.**
* If notifications are sent while nobody waits on the condition variable, it is
simply discarded, therefore
* a producer-consumer scenario is more robust if it tends to send "too many"
notifications (of which some are discarded) …
* … while sending "too few" could cause some thread to wait forever.
.N[
Specifying the condition check in combination with `wait`-ing *does it right*
and hence should be preferred over writing a loop explicitly.._[]
]
[Spurious Wake-Up]: http://en.wikipedia.org/wiki/Spurious_wakeup
.F[:
Thus avoiding some later maintainer considers the `while` "unnecessary" and
replace it with `if` …
]
---
template: plain
name: atomic_operations
header: ### Atomic Operation Support
With the support for atomic operations C++11 multi-threading allows to implement
* [Wait-Free Algorithms](http://en.wikipedia.org/wiki/Non-blocking_algorithm#Wait-freedom),
* [Lock-Free Algorithms](http://en.wikipedia.org/wiki/Non-blocking_algorithm#Lock-freedom),
and
* [Obstruction-Free Algorithms](http://en.wikipedia.org/wiki/Non-blocking_algorithm#Obstruction-freedom).
The basic concept is to provide a way to know whether a certain modification of
some memory location was caused by the current thread or by another one.
.I[
For more information about atomic operation support see:
http://en.cppreference.com/w/cpp/atomic
]
---
template: plain
name: atomic_operations_example
header: #### Atomic Operations Example
The following example, demonstrating the lock-free approach with operations
(instead of using mutexes) modifies a [former example](#mutex_example_1):
```
template<typename T1, typename T2, typename Transformation>
void worker(T1 args[], std::size_t data_size, T2 result,
std::atomic_size_t &total_progress,
Transformation func) {
while (total_progress < data_size) {
constexpr auto chunks = std::size_t{100};
std::size_t beg = total_progress.load();
std::size_t end;
do {
end = ((data_size - beg) > chunks)
? beg + chunks
: data_size;
} while (!total_progress.compare_exchange_weak(beg, end));
std::transform(&data[beg], &data[end], &sqrts[beg], func);
}
}
```
.N[
Be sure to understand that the loop controlled by the return value of
`compare_exchange_weak` guarantees the prior calculations are (still) valid.
]
---
template: plain
name: memory_order
header: #### Memory Order (and Dependencies)
Memory order *"specifies how regular, non-atomic memory accesses are to be ordered
around an atomic operation*".._[]
There are a number of different models to select from,
* with the default providing *"sequentially consistent ordering"*,
* being closest to what most developers would expect,
* but (possibly) not with the optimal performance for a given use case.
.I[
For more information on memory order see:
http://en.cppreference.com/w/cpp/atomic/memory_order
]
.F[:
Cited from the reference further down on this page.
]
---
template: plain
name: atomic_operations
header: #### Atomic Operations Recommendation
.N[
* Except for the potential of deadlocks the challenges are similar to algorithms
using locks:._[]
* Problems may only show for a critical timing and may be reproducible in
particular test environments only.
* In general, a failed test may show the presence of errors, but even many
successful tests do not guarantee their absence.
]
.W[
Beyond trivial cases – like the one shown in the example –, implementing
multi-threaded programs with atomic operations requires **substantial
expertise**.
Be sure to keep the design **as simple as possible** and have it reviewed by
other developers experienced in that particular fields, maybe both, colleagues
and hired consultants too.
]
.F[:
It may very well be the case that problematic situations depend on hardware
features like the size of cache-lines, the depth of an instruction pipeline,
or the way branch prediction works.
]
---
template: plain
name: direct_use_of_threads
header: ### Using Class `std::thread`
In the examples before the class `std::thread` was used only indirectly (via
`std::async`, building on [Futures and Promises](#futures_and_promises).
* There is also a class `std::thread`
* taking any runnable code as constructor argument,
* executing it in a separate thread.
.I[
For more information on the `std::thread` class see:
http://en.cppreference.com/w/cpp/thread
]
**There are explicitly no means provided to forcefully terminate one thread
from another one.**
.W[
Any non-portable way._[] to forcefully terminate a thread risks to entail
serious consequences later, as e.g. locks may not be released or awaited
notifications not be sent.
]
.F[:
Such as potentially existing *interrupt* or *kill* functions reachable via a