C++11 is the first C++ standard that deals with concurrency. The story goes on with C++17 and will continue with C++20/23.
I'll give you a detailed insight into the current and the upcoming concurrency in C++. This insight includes the theory and a lot of practice.
Author(s): Rainer Grimm
Publisher: leanpub
Year: 2019
Language: English
Pages: 475
Tags: C++, Concurrency
Table of Contents......Page 4
Reader Testimonials......Page 12
Special Symbols......Page 14
Run the Programs......Page 15
Acknowledgements......Page 16
My Special Circumstances......Page 17
A Quick Overview......Page 18
Memory Model......Page 19
Multithreading......Page 20
C++17: Parallel Algorithms of the Standard Template Library......Page 22
Ongoing Optimisation with CppMem......Page 23
std::jthread......Page 24
Transactional Memory......Page 25
Best Practices......Page 26
Glossary......Page 27
The Details......Page 28
Memory Model......Page 29
What is a memory location?......Page 30
What happens if two threads access the same memory location?......Page 31
The Contract......Page 32
The Challenges......Page 33
Strong versus Weak Memory Model......Page 36
The Atomic Flag......Page 38
The Class Template std::atomic......Page 44
All Atomic Operations......Page 55
Free Atomic Functions......Page 56
std::shared_ptr......Page 57
The Six Variants of Memory Orderings in C++......Page 60
Sequential Consistency......Page 62
Acquire-Release Semantic......Page 64
std::memory_order_consume......Page 76
Relaxed Semantic......Page 81
std::atomic_thread_fence......Page 84
std::atomic_signal_fence......Page 94
Multithreading......Page 96
Thread Creation......Page 97
Thread Lifetime......Page 98
Thread Arguments......Page 101
Methods......Page 105
Shared Data......Page 109
Mutexes......Page 111
Locks......Page 118
Thread-safe Initialisation......Page 128
Thread-Local Data......Page 135
Condition Variables......Page 139
The Predicate......Page 141
The Wait Workflow......Page 143
Tasks versus Threads......Page 145
std::async......Page 147
std::packaged_task......Page 153
std::promise and std::future......Page 158
std::shared_future......Page 163
Exceptions......Page 168
Notifications......Page 171
Parallel Algorithms of the Standard Template Library......Page 174
Execution Policies......Page 175
Parallel and Vectorised Execution......Page 176
Exceptions......Page 177
Hazards of Data Races and Deadlocks......Page 179
The New Algorithms......Page 181
The functional Heritage......Page 188
Performance......Page 189
Case Studies......Page 193
Single Threaded addition of a Vector......Page 194
Multithreaded Summation with a Shared Variable......Page 200
Thread-Local Summation......Page 206
Summation of a Vector: The Conclusion......Page 217
Double-Checked Locking Pattern......Page 219
Performance Measurement......Page 221
Thread-Safe Meyers Singleton......Page 223
std::lock_guard......Page 225
std::call_once with std::once_flag......Page 227
Atomics......Page 228
Performance Numbers of the various Thread-Safe Singleton Implementations......Page 232
Ongoing Optimisation with CppMem......Page 233
CppMem: Non-Atomic Variables......Page 235
CppMem: Locks......Page 240
CppMem: Atomics with Sequential Consistency......Page 241
CppMem: Atomics with Acquire-Release Semantic......Page 249
CppMem: Atomics with Non-atomics......Page 252
CppMem: Atomics with Relaxed Semantic......Page 254
Conclusion......Page 257
The Future: C++20/23......Page 258
Executors......Page 259
What is an Executor?......Page 260
First Examples......Page 261
Goals of an Executor Concept......Page 264
Execution Functions......Page 265
A Prototype Implementation......Page 267
Automatically Joining......Page 271
Interrupt a std::jthread......Page 273
Stop Tokens......Page 275
Atomic Smart Pointers......Page 281
A thread-safe singly linked list......Page 282
Concurrency TS v1......Page 283
Unified Futures......Page 288
std::latch......Page 294
std::barrier......Page 295
std::flex_barrier......Page 296
A Generator Function......Page 299
Details......Page 302
ACI(D)......Page 305
Synchronized and Atomic Blocks......Page 306
transaction_safe versus transaction_unsafe Code......Page 309
Fork and Join......Page 310
define_task_block versus define_task_block_restore_thread......Page 311
The Interface......Page 312
The Scheduler......Page 313
Patterns......Page 314
History......Page 315
Pattern versus Best Practices......Page 317
Anti-Pattern......Page 318
Copied Value......Page 319
Thread-Specific Storage......Page 325
Future......Page 327
Scoped Locking......Page 328
Strategized Locking......Page 330
Thread-Safe Interface......Page 339
Guarded Suspension......Page 346
Active Object......Page 353
Components......Page 354
Advantages and Disadvantages......Page 356
Implementation......Page 357
Further Information......Page 362
Components......Page 363
Runtime behaviour......Page 364
Advantages and Disadvantages......Page 365
Further Information......Page 369
Half-Sync/Half-Async......Page 370
Reactor......Page 371
Proactor......Page 374
Further Information......Page 376
Code Reviews......Page 377
Minimise Sharing of Mutable Data......Page 379
Minimise Waiting......Page 381
Prefer Immutable Data......Page 382
Use pure functions......Page 385
Look for the Right Abstraction......Page 386
Use Dynamic Enforcement Tools......Page 387
Threads......Page 388
Data Sharing......Page 393
Condition Variables......Page 401
Promises and Futures......Page 404
If you program Lock-Free, use well-established patterns......Page 405
Don't reinvent the wheel......Page 406
Data Structures......Page 407
Lock-Based Data Structures......Page 408
Lock-Free Data Structures......Page 409
Further Information......Page 410
ABA Problem......Page 411
Blocking Issues......Page 415
Breaking of Program Invariants......Page 417
Data Races......Page 418
Deadlocks......Page 420
False Sharing......Page 421
Lifetime Issues of Variables......Page 426
Moving Threads......Page 427
Race Conditions......Page 429
The Time Library......Page 430
The Interplay of Time Point, Time Duration, and Clock......Page 431
From Time Point to Calendar Time......Page 432
Cross the valid Time Range......Page 434
Time Duration......Page 436
Calculations......Page 438
Accuracy and Steadiness......Page 441
Epoch......Page 443
Sleep and Wait......Page 447
The simplified Overview......Page 453
2. Program......Page 454
The Examples......Page 455
Concurrency......Page 461
Function Objects......Page 462
Math Laws......Page 463
Monad......Page 464
Non-blocking......Page 465
Release Sequence......Page 466
Total order......Page 467
wait-free......Page 468
Index......Page 469