Share E-Book
Scan to open this page

Scan with your phone to open this page

Author: Rainer Grimm

Rating No ratings yet

C++11 is the first C++ standard that deals with concurrency. The story goes on with C++17, C++20, and will continue with C++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 C++11 and C++14 have the basic building blocks for creating concurrent or parallel programs. With C++17, we got the parallel algorithms of the Standard Template Library (STL). That means most of the algorithms of the STL can be executed sequentially, in parallel, or vectorized. The concurrency story in C++ goes on. With C++20, we got coroutines, atomic smart pointers, semaphores, latches, and barriers. C++23 supports the first concrete coroutine: std::generator. With future C++ standards, we can hope for executors, extended futures, transactional memory, and more. This book explains the details of concurrency in modern C++ and gives you nearly 200 running code examples. Therefore, you can combine theory with practice and get the most out of it. Because this book is about concurrency, I present many pitfalls and show you how to overcome them. The book is 100 % finished, but I will update it regularly. The next update is probably about C++26. Furthermore, I will write about lock-free concurrent data structure and patterns for parallelization.

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
【One-Line Pitch】 A practical, standard-by-standard tour of concurrency in modern C++—from the C++11 memory model and threads through C++17 parallel algorithms, C++20 coroutines and synchronization primitives, to C++23 and beyond—for working C++ programmers who want theory backed by runnable examples and a clear map of the pitfalls. 【Book Arc】 - **Opening (~0%–15%)**: Orients you in the book's structure and the evolution of C++ concurrency across standards, then lays out the memory model—memory locations, the contract, atomics, and the six memory orderings—as the conceptual foundation everything else rests on. - **Early (~15%–30%)**: Moves into hands-on multithreading: `std::thread` and C++20's `std::jthread`, mutexes and locks, thread-local data, condition variables, cooperative interruption, semaphores, latches, and barriers—the everyday toolkit for writing correct concurrent code. - **Early–Middle (~28%–35%)**: Covers C++17 parallel STL algorithms (sequential, parallel, vectorized execution) and then C++20 coroutines—generator functions, the promise/awaiter framework, and the `co_return`/`co_yield`/`co_await` workflows—culminating in C++23's `std::generator`. - **Middle (~35%–50%)**: Reinforces everything through extended case studies—vector summation, the dining philosophers solved many ways, thread-safe singletons, job workflows, and a thread-safe queue—then previews the future: executors, extended futures, transactional memory, task blocks, and data-parallel vectors. - **Late (~48%–55%)**: Shifts from mechanisms to design with synchronization patterns (dealing with sharing and mutation, scoped and strategized locking, thread-safe interface, guarded suspension) and concurrent architectures (Active Object, Monitor Object). - **Ending (~55%+)**: The excerpts do not cover the closing material in detail; the table of contents indicates patterns and best practices continue, and the author notes plans to add lock-free data structures and parallelization patterns in future updates. 【Key Takeaways】 - **The memory model is the bedrock** (Opening): Understanding memory locations, atomics, and ordering constraints (sequential consistency, acquire-release, relaxed) is what separates code that works from code that merely appears to. This is the hardest and most important part. - **Modern C++ gives you a layered toolkit** (Early): `std::thread`/`std::jthread`, mutexes, locks, condition variables, and C++20's semaphores, latches, and barriers each solve a distinct coordination problem—knowing which to reach for is the skill. - **C++20's `std::jthread` and stop tokens simplify cancellation** (Early): Automatic joining and cooperative interruption via `stop_source`/`stop_token`/`stop_callback` replace error-prone manual thread lifecycle management. - **Parallel STL algorithms make data parallelism accessible** (Early–Middle): Most STL algorithms can run sequentially, in parallel, or vectorized, but performance varies by compiler and workload—measure rather than assume. - **Coroutines are a framework, not a keyword** (Early–Middle): Promise objects, awaiters, and the suspend/resume workflows are the machinery behind `co_await`; understanding them is essential before writing your own coroutine types. - **Case studies teach more than rules** (Middle): The dining philosophers solved a dozen ways and the vector-summation variants show how the same problem demands different synchronization strategies—and where each fails. - **Patterns turn knowledge into architecture** (Late): Scoped/strategized locking, thread-safe interface, guarded suspension, Active Object, and Monitor Object give you reusable designs rather than ad-hoc fixes. - **Pitfalls are first-class content** (Throughout): The book deliberately presents common errors—lost/spurious wakeups, deadlocks, busy-waiting mistakes—and shows how to overcome them, which is where real learning happens. 【Reading Tips】 - **Deep-read the memory model chapter** (roughly the first fifth). Skim it at your peril; everything later assumes you understand ordering and atomics. - **Type along with the ~200 examples.** Concurrency bugs are invisible on the page—compile, run, and deliberately break the code to see failures. - **Use the case studies as checkpoints.** After the multithreading and coroutine chapters, work through the dining philosophers and vector summation to test whether the primitives have clicked. - **Skim the "Future of C++" chapter on a first pass.** Executors, transactional memory, and task blocks are forward-looking; return to them when you need to design beyond current standards. - **Treat patterns as a reference.** Read the synchronization and architecture patterns when you face a design decision, not necessarily front-to-back. 【Coverage Limits】 This guide is based on stratified excerpts that are heavy on the table of contents and structural markers; detailed prose from the patterns, architecture, and closing chapters is not well represented, so specific claims about those sections are inferred from headings rather than full text.
Page 2
ers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many i...
View in text
Page 6
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 3.2.2 Cooperative Interruption of a std::jthread . . . . . . . . . . . . . . . . . . 119 3.3...
View in text
Page 7
std::suspend_always and std::suspend_never . . . . . . . . . . . . . . . . . 257 5.4.4 initial_suspend . . . . . . . . . . . . . . . . . . . . . . . . . . ....
View in text
Page 9
. . . . . . . . . . . . . . . . . . . . . . . . . . 443 7.3.2 Synchronized and Atomic Blocks . . . . . . . . . . . . . . . . . . . . . . . . 444 7.3.3 transa...
View in text
Page 11
. . . . . . . . . 533 10.6 Further Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537 11. Best Practices . . . . . . . ....
View in text
Page 13
. 692 17.1.3 3. Display Relations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693 17.1.4 4. Display Layout . . . . . . . . . . . . . . . ....
View in text
Page 16
t way to become an expert in the multithreading environment. This book contains both simple and advanced topics, and it has everything a developer needs, in...
View in text
Page 20
dernes Cpp⁶ and the German blog⁷, hosted by Heise Developer. Since 2016, I have been an independent instructor and mentor giving seminars about modern C++ an...
View in text
Tags
AI categories
Programming LanguageC++Backend
ISBN: 8873420931
Publisher: LeanPub
Publish Year: 2024
Language: English
Pages: 737
File Format: PDF
File Size: 12.3 MB
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Generating text preview…