Concurrency with Modern C++
Minimum price
Suggested price

Concurrency with Modern C++

What every professional C++ programmer should know about concurrency.

About the Book

  • 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.

About the Author

Rainer Grimm
Rainer Grimm

I've worked as a software architect, team lead, and instructor since 1999. In 2002, I created a further education round at my company. I have given training courses since 2002. My first tutorials were about proprietary management software, but soon after, I began teaching Python and C++. In my spare time, I like to write articles about C++, Python, and Haskell. I also like to speak at conferences. I publish weekly on my English blog and the, hosted by Heise Developer.

Since 2016, I have been an independent instructor giving seminars about modern C++ and Python. I have published several books in various languages about modern C++ and in particular, concurrency. Due to my profession, I always search for the best way to teach modern C++.

My books "C++ 11 für Programmierer ", "C++" and "C++ Standardbibliothek kurz & gut" for the "kurz & gut" series were published by Pearson and O'Reilly. They are available in German, English, Korean, and Persian. In summer 2018 I published a new book on Leanpub: "Concurrency with Modern C++". This book is also available in German: "Modernes C++: Concurrency meistern".


The Book
  • PDF

  • EPUB

  • WEB

  • English

Minimum price
Suggested price
Concurreny with Modern C++ Team Edition: Five Copies

Get five copies to the price of three. This package includes all code examples.

  • PDF

  • EPUB

  • WEB

  • English

Minimum price
Suggested price

Bundles that include this book

Bought separately
Bundle Price
Bought separately
Bundle Price
Bought separately
Bundle Price

Reader Testimonials

Bart Vandewoestyne
Bart Vandewoestyne

Senior Development Engineer Software at Esterline

'Concurrency with Modern C++' is your practical guide to getting familiar with concurrent programming in Modern C++. Starting with the C++ Memory Model and using many ready-to-run code examples, the book covers a good deal of what you need to improve your C++ multithreading skills. Next to the enlightening case studies that will bring you up to speed, the overview of upcoming concurrency features might even wet your appetite for more!

Ian Reeve
Ian Reeve

Senior Storage Software Engineer for Dell Inc.

Rainer Grimm's Concurrency with Modern C++ is a well written book covering the theory and practice for working with concurrency per the existing C++ standards, as well as addressing the potential changes for the upcoming C++ 20 standard. He provides a conversational discussion of the applications and best practices for concurrency along with example code to reinforce the details of each topic. An informative and worthwhile read!

Robert Badea
Robert Badea

Technical Team Leader

Concurrency with Modern C++ is the easiest 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 order to become an expert in this field: Lots of content, a big number of running code examples, along with great explanation, and a whole chapter for pitfalls. I enjoyed reading it, and I highly recommend it for everyone working with C++.

Zeshuang Mi
Zeshuang Mi


Concurrency with Modern C++ makes the multithreading's concept so clear. At the beginning of this book, it makes me know very clearly about memory model. The memory model is very important to the multithreading. The book makes me have confidence when i write my concurrent program, which makes me know what should i do or what i shouldn't do. The author elaborates the multithreading pattern which makes so helpful to me. I love it so much.

Table of Contents

    • Introduction
      • Conventions
        • Special Fonts
        • Special Symbols
        • Special Boxes
      • Source Code
        • Run the Programs
      • How should you read the book?
      • Personal Notes
        • Acknowledgment
        • About Me
  • A Quick Overview
    • Concurrency with Modern C++
      • C++11 and C++14: The Foundation
        • Memory Model
        • Multithreading
      • C++17: Parallel Algorithms of the Standard Template Library
        • Execution Policy
        • New Algorithms
      • Coroutines
      • Case Studies
        • Calculating the Sum of a Vector
        • The Dining Philosophers Problem by Andre Adrian
        • Thread-Safe Initialization of a Singleton
        • Ongoing Optimization with CppMem
        • Fast Synchronization of Threads
      • Variations of Futures
      • Modification and Generalization of a Generator
      • Various Job Workflows
      • The Future of C++
        • Executors
        • Extended futures
        • Transactional Memory
        • Task Blocks
        • Data-Parallel Vector Library
      • Patterns and Best Practices
        • Synchronization
        • Concurrent Architecture
        • Best Practices
      • Data Structures
      • Challenges
      • Time Library
      • CppMem
      • Glossary
  • The Details
    • Memory Model
      • Basics of the Memory Model
        • What is a memory location?
        • What happens if two threads access the same memory location?
      • The Contract
        • The Foundation
        • The Challenges
      • Atomics
        • Strong versus Weak Memory Model
        • The Atomic Flag
        • std::atomic
        • All Atomic Operations
        • Free Atomic Functions
        • std::atomic_ref (C++20)
      • The Synchronization and Ordering Constraints
        • The Six Variants of Memory Orderings in C++
        • Sequential Consistency
        • Acquire-Release Semantic
        • std::memory_order_consume
        • Relaxed Semantic
      • Fences
        • std::atomic_thread_fence
        • std::atomic_signal_fence
    • Multithreading
      • The Basic Thread std::thread
        • Thread Creation
        • Thread Lifetime
        • Thread Arguments
        • Member Functions
      • The Improved Thread std::jthread (C++20)
        • Automatically Joining
        • Cooperative Interruption of a std::jthread
      • Shared Data
        • Mutexes
        • Locks
        • std::lock
        • Thread-safe Initialization
      • Thread-Local Data
      • Condition Variables
        • The Predicate
        • Lost Wakeup and Spurious Wakeup
        • The Wait Workflow
      • Cooperative Interruption (C++20)
        • std::stop_source
        • std::stop_token
        • std::stop_callback
        • A General Mechanism to Send Signals
        • Additional Functionality of std::jthread
        • New wait Overloads for the condition_variable_any
      • Semaphores (C++20)
      • Latches and Barriers (C++20)
        • std::latch
        • std::barrier
      • Tasks
        • Tasks versus Threads
        • std::async
        • std::packaged_task
        • std::promise and std::future
        • std::shared_future
        • Exceptions
        • Notifications
      • Synchronized Outputstreams (C++20)
    • Parallel Algorithms of the Standard Template Library
      • Execution Policies
        • Parallel and Vectorized Execution
        • Exceptions
        • Hazards of Data Races and Deadlocks
      • Algorithms
      • The New Algorithms
        • More overloads
        • The functional Heritage
      • Compiler Support
        • Microsoft Visual Compiler
        • GCC Compiler
        • Further Implementations of the Parallel STL
      • Performance
        • Microsoft Visual Compiler
        • GCC Compiler
    • Coroutines (C++20)
      • A Generator Function
      • Characteristics
        • Typical Use Cases
        • Underlying Concepts
        • Design Goals
        • Becoming a Coroutine
      • The Framework
        • Promise Object
        • Coroutine Handle
        • Coroutine Frame
      • Awaitables and Awaiters
        • Awaitables
        • The Concept Awaiter
        • std::suspend_always and std::suspend_never
        • initial_suspend
        • final_suspend
        • Awaiter
      • The Workflows
        • The Promise Workflow
        • The Awaiter Workflow
      • co_return
        • A Future
      • co_yield
        • An Infinite Data Stream
      • co_await
        • Starting a Job on Request
        • Thread Synchronization
        • std::generator (C++23)
    • Case Studies
      • Calculating the Sum of a Vector
        • Single Threaded addition of a Vector
        • Multithreaded Summation with a Shared Variable
        • Thread-Local Summation
        • Summation of a Vector: The Conclusion
      • The Dining Philosophers Problem by Andre Adrian
        • Multiple Resource Use
        • Multiple Resource Use with Logging
        • Erroneous Busy Waiting without Resource Hierarchy
        • Erroneous Busy Waiting with Resource Hierarchy
        • Still Erroneous Busy Waiting with Resource Hierarchy
        • Correct Busy Waiting with Resource Hierarchy
        • Good low CPU load Busy Waiting with Resource Hierarchy
        • std::mutex with Resource Hierarchy
        • std::lock_guard with Resource Hierarchy
        • std::lock_guard and Synchronized Output with Resource Hierarchy
        • std::lock_guard and Synchronized Output with Resource Hierarchy and a count
        • A std::unique_lock using deferred locking
        • A std::scoped_lock with Resource Hierarchy
        • The Original Dining Philosophers Problem using Semaphores
        • A C++20 Compatible Semaphore
      • Thread-Safe Initialization of a Singleton
        • Double-Checked Locking Pattern
        • Performance Measurement
        • Thread-Safe Meyers Singleton
        • std::lock_guard
        • std::call_once with std::once_flag
        • Atomics
        • Performance Numbers of the various Thread-Safe Singleton Implementations
      • Ongoing Optimization with CppMem
        • CppMem: Non-Atomic Variables
        • CppMem: Locks
        • CppMem: Atomics with Sequential Consistency
        • CppMem: Atomics with Acquire-Release Semantic
        • CppMem: Atomics with Non-atomics
        • CppMem: Atomics with Relaxed Semantic
        • Conclusion
      • Fast Synchronization of Threads
        • Condition Variables
        • std::atomic_flag
        • std::atomic<bool>
        • Semaphores
        • All Numbers
      • Variations of Futures
        • A Lazy Future
        • Execution on Another Thread
      • Modification and Generalization of a Generator
        • Modifications
        • Generalization
      • Various Job Workflows
        • The Transparent Awaiter Workflow
        • Automatically Resuming the Awaiter
        • Automatically Resuming the Awaiter on a Separate Thread
    • The Future of C++
      • Executors
        • A long Way
        • What is an Executor?
        • First Examples
        • Goals of an Executor Concept
        • Terminology
        • Execution Functions
        • A Prototype Implementation
      • Extended Futures
        • Concurrency TS v1
        • Unified Futures
      • Transactional Memory
        • ACI(D)
        • Synchronized and Atomic Blocks
        • transaction_safe versus transaction_unsafe Code
      • Task Blocks
        • Fork and Join
        • define_task_block versus define_task_block_restore_thread
        • The Interface
        • The Scheduler
      • Data-Parallel Vector Library
        • Data-Parallel Vectors
        • The Interface of the Data-Parallel Vectors
  • Patterns
    • Patterns and Best Practices
      • History
      • Invaluable Value
      • Pattern versus Best Practices
      • Anti-Pattern
    • Synchronization Patterns
      • Dealing with Sharing
        • Copied Value
        • Thread-Specific Storage
        • Future
      • Dealing with Mutation
        • Scoped Locking
        • Strategized Locking
        • Thread-Safe Interface
        • Guarded Suspension
    • Concurrent Architecture
      • Active Object
        • Challenges
        • Solution
        • Components
        • Dynamic Behavior
        • Advantages and Disadvantages
        • Implementation
      • Monitor Object
        • Challenges
        • Solution
        • Components
        • Dynamic Behavior
        • Advantages and Disadvantages
      • Half-Sync/Half-Async
        • Challenges
        • Solution
        • Components
        • Dynamic Behavior
        • Advantages and Disadvantages
        • Example
      • Reactor
        • Challenges
        • Solution
        • Components
        • Dynamic Behavior
        • Advantages and Disadvantages
        • Example
      • Proactor
        • Challenges
        • Solution
        • Components
        • Advantages and Disadvantages
        • Example
      • Further Information
    • Best Practices
      • General
        • Code Reviews
        • Minimize Sharing of Mutable Data
        • Minimize Waiting
        • Prefer Immutable Data
        • Use pure functions
        • Look for the Right Abstraction
        • Use Static Code Analysis Tools
        • Use Dynamic Enforcement Tools
      • Multithreading
        • Threads
        • Data Sharing
        • Condition Variables
        • Promises and Futures
      • Memory Model
        • Don’t use volatile for synchronization
        • Don’t program Lock Free
        • If you program Lock-Free, use well-established patterns
        • Don’t build your abstraction, use guarantees of the language
        • Don’t reinvent the wheel
  • Data Structures
    • General Considerations
      • Concurrent Stack
      • Locking Strategy
      • Granularity of the Interface
      • Typical Usage Pattern
        • Linux (GCC)
        • Windows (cl.exe)
      • Avoidance of Loopholes
      • Contention
        • Single-Threaded Summation without Synchronization
        • Single-Threaded Summation with Synchronization (lock)
        • Single-Threaded Summation with Synchronization (atomic)
        • The Comparison
      • Scalability
      • Invariants
      • Exceptions
    • Lock-Based Data Structures
        • A Stack
      • Concurrent Queue
        • A Queue
        • Coarse-Grained Locking
        • Fine-Grained Locking
    • Lock-Free Data Structures
      • General Considerations
        • The Next Evolutionary Step
        • Sequential Consistency
      • Concurrent Stack
        • A Simplified Implementation
        • A Complete Implementation
      • Concurrent Queue
  • Further Information
    • Challenges
      • ABA Problem
      • Blocking Issues
      • Breaking of Program Invariants
      • Data Races
      • Deadlocks
      • False Sharing
      • Lifetime Issues of Variables
      • Moving Threads
      • Race Conditions
    • The Time Library
      • The Interplay of Time Point, Time Duration, and Clock
      • Time Point
        • From Time Point to Calendar Time
        • Cross the valid Time Range
      • Time Duration
        • Calculations
      • Clocks
        • Accuracy and Steadiness
        • Epoch
      • Sleep and Wait
    • CppMem - An Overview
      • The simplified Overview
        • 1. Model
        • 2. Program
        • 3. Display Relations
        • 4. Display Layout
        • 5. Model Predicates
        • The Examples
    • Glossary
      • adress_free
      • ACID
      • CAS
      • Callable Unit
      • Complexity
      • Concepts
      • Concurrency
      • Critical Section
      • Deadlock
      • Eager Evaluation
      • Executor
      • Function Objects
      • Lambda Functions
      • Lazy evaluation
      • Lock-free
      • Lock-based
      • Lost Wakeup
      • Math Laws
      • Memory Location
      • Memory Model
      • Modification Order
      • Monad
      • Non-blocking
      • obstruction-free
      • Parallelism
      • Predicate
      • Pattern
      • RAII
      • Release Sequence
      • Sequential Consistency
      • Sequence Point
      • Spurious Wakeup
      • Thread
      • Total order
      • TriviallyCopyable
      • Undefined Behavior
      • volatile
      • wait-free
    • Index

The Leanpub 60-day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms

80% Royalties. Earn $16 on a $20 book.

We pay 80% royalties. That's not a typo: you earn $16 on a $20 sale. If we sell 5000 non-refunded copies of your book or course for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earnedover $12 million USDwriting, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub