Concurrency with Modern C++
$33.00
Minimum price
$41.00
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 sequential, parallel, or vectorized.
  • The concurrency story in C++ goes on. With C++20, we got coroutines, atomic smart pointers, semaphores, and latches and barriers.
  • With C++23, we can hope for executors, extended futures, transactional memory, and more.

This book explains the details of concurrency in modern C++ and gives you, also, nearly 200 running code examples. Therefore, you can combine the theory with the practices and get the most 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 from time to time. The next update is probably about C++23. Furthermore, I will start the chapter on lock-free data structures.

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 https://www.modernescpp.com and the https://www.grimm-jaud.de/index.php/blog, 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".

Packages

The Book

Includes:

  • extras
    Source code

    All source code examples.

  • PDF

  • EPUB

  • MOBI

  • WEB

  • English

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

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

Includes:

  • extras
    Source code

    All source code examples.

  • PDF

  • EPUB

  • MOBI

  • WEB

  • English

$99.00
Minimum price
$123.00
Suggested price

Bundles that include this book

$61.00
Suggested Price
$46.00
Bundle Price
$100.94
Suggested Price
$54.99
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

Postgraduate

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

  •  
    • Reader Testimonials
    • Introduction
      • Conventions
        • Special Fonts
        • Special Symbols
        • Special Boxes
      • Source Code
        • Run the Programs
      • How should you read the book?
      • Personal Notes
        • Acknowledgements
        • About Me
        • My Special Circumstances
  • A Quick Overview
    • 1. Concurrency with Modern C++
      • 1.1 C++11 and C++14: The Foundation
        • 1.1.1 Memory Model
        • 1.1.2 Multithreading
      • 1.2 C++17: Parallel Algorithms of the Standard Template Library
        • 1.2.1 Execution Policy
        • 1.2.2 New Algorithms
      • 1.3 The Near Future: C++20
      • 1.4 Coroutines
      • 1.5 Case Studies
        • 1.5.1 Calculating the Sum of a Vector
        • 1.5.2 Thread-Safe Initialisation of a Singleton
        • 1.5.3 Ongoing Optimisation with CppMem
        • 1.5.4 Fast Synchronization of Threads
      • 1.6 Variations of Futures
      • 1.7 Modification and Generalization of a Generator
      • 1.8 Various Job Workflows
      • 1.9 The Future: C++23
        • 1.9.1 Executors
        • 1.9.2 Extended futures
        • 1.9.3 Transactional Memory
        • 1.9.4 Task Blocks
        • 1.9.5 Data-Parallel Vector Library
      • 1.10 Patterns and Best Practices
        • 1.10.1 Synchronization
        • 1.10.2 Concurrent Architecture
        • 1.10.3 Best Practices
      • 1.11 Data Structures
      • 1.12 Challenges
      • 1.13 Time Library
      • 1.14 CppMem
      • 1.15 Glossary
  • The Details
    • 2. Memory Model
      • 2.1 Basics of the Memory Model
        • 2.1.1 What is a memory location?
        • 2.1.2 What happens if two threads access the same memory location?
      • 2.2 The Contract
        • 2.2.1 The Foundation
        • 2.2.2 The Challenges
      • 2.3 Atomics
        • 2.3.1 Strong versus Weak Memory Model
        • 2.3.2 The Atomic Flag
        • 2.3.3 The Class Template std::atomic
        • 2.3.4 All Atomic Operations
        • 2.3.5 Free Atomic Functions
        • 2.3.6 The Class Template std::atomic_ref (C++20)
      • 2.4 The Synchronisation and Ordering Constraints
        • 2.4.1 The Six Variants of Memory Orderings in C++
        • 2.4.2 Sequential Consistency
        • 2.4.3 Acquire-Release Semantic
        • 2.4.4 std::memory_order_consume
        • 2.4.5 Relaxed Semantic
      • 2.5 Fences
        • 2.5.1 std::atomic_thread_fence
        • 2.5.2 std::atomic_signal_fence
    • 3. Multithreading
      • 3.1 The Basic Thread std::thread
        • 3.1.1 Thread Creation
        • 3.1.2 Thread Lifetime
        • 3.1.3 Thread Arguments
        • 3.1.4 Member Functions
      • 3.2 The Improved Thread std::jthread (C++20)
        • 3.2.1 Automatically Joining
        • 3.2.2 Cooperative Interruption of a std::jthread
      • 3.3 Shared Data
        • 3.3.1 Mutexes
        • 3.3.2 Locks
        • 3.3.3 Thread-safe Initialisation
      • 3.4 Thread-Local Data
      • 3.5 Condition Variables
        • 3.5.1 The Predicate
        • 3.5.2 Lost Wakeup and Spurious Wakeup
        • 3.5.3 The Wait Workflow
      • 3.6 Cooperative Interruption (C++20)
        • 3.6.1 std::stop_source
        • 3.6.2 std::stop_token
      • 3.7 Semaphores (C++20)
      • 3.8 Latches and Barriers (C++20)
        • 3.8.1 std::latch
        • 3.8.2 std::barrier
      • 3.9 Tasks
        • 3.9.1 Tasks versus Threads
        • 3.9.2 std::async
        • 3.9.3 std::packaged_task
        • 3.9.4 std::promise and std::future
        • 3.9.5 std::shared_future
        • 3.9.6 Exceptions
        • 3.9.7 Notifications
      • 3.10 Synchronized Outputstreams (C++20)
    • 4. Parallel Algorithms of the Standard Template Library
      • 4.1 Execution Policies
        • 4.1.1 Parallel and Vectorised Execution
        • 4.1.2 Exceptions
        • 4.1.3 Hazards of Data Races and Deadlocks
      • 4.2 Algorithms
      • 4.3 The New Algorithms
        • 4.3.1 More overloads
        • 4.3.2 The functional Heritage
      • 4.4 Performance
    • 5. Coroutines (C++20)
      • 5.1 A Generator Function
      • 5.2 Characteristics
        • 5.2.1 Typical Use Cases
        • 5.2.2 Underlying Concepts
        • 5.2.3 Design Goals
        • 5.2.4 Becoming a Coroutine
      • 5.3 The Framework
        • 5.3.1 Promise Object
        • 5.3.2 Coroutine Handle
        • 5.3.3 Coroutine Frame
      • 5.4 Awaitables and Awaiters
        • 5.4.1 Awaitables
        • 5.4.2 The Concept Awaitable
        • 5.4.3 std::suspend_always and std::suspend_never
        • 5.4.4 initial_suspend
        • 5.4.5 final_suspend
        • 5.4.6 Awaiter
      • 5.5 The Workflows
        • 5.5.1 The Promise Workflow
        • 5.5.2 The Awaiter Workflow
      • 5.6 co_return
        • 5.6.1 A Future
      • 5.7 co_yield
        • 5.7.1 An Infinite Data Stream
      • 5.8 co_await
        • 5.8.1 Starting a Job on Request
        • 5.8.2 Thread Synchronization
    • 6. Case Studies
      • 6.1 Calculating the Sum of a Vector
        • 6.1.1 Single Threaded addition of a Vector
        • 6.1.2 Multithreaded Summation with a Shared Variable
        • 6.1.3 Thread-Local Summation
        • 6.1.4 Summation of a Vector: The Conclusion
      • 6.2 Thread-Safe Initialisation of a Singleton
        • 6.2.1 Double-Checked Locking Pattern
        • 6.2.2 Performance Measurement
        • 6.2.3 Thread-Safe Meyers Singleton
        • 6.2.4 std::lock_guard
        • 6.2.5 std::call_once with std::once_flag
        • 6.2.6 Atomics
        • 6.2.7 Performance Numbers of the various Thread-Safe Singleton Implementations
      • 6.3 Ongoing Optimisation with CppMem
        • 6.3.1 CppMem: Non-Atomic Variables
        • 6.3.2 CppMem: Locks
        • 6.3.3 CppMem: Atomics with Sequential Consistency
        • 6.3.4 CppMem: Atomics with Acquire-Release Semantic
        • 6.3.5 CppMem: Atomics with Non-atomics
        • 6.3.6 CppMem: Atomics with Relaxed Semantic
        • 6.3.7 Conclusion
      • 6.4 Fast Synchronisation of Threads
        • 6.4.1 Condition Variables
        • 6.4.2 std::atomic_flag
        • 6.4.3 std::atomic<bool>
        • 6.4.4 Semaphores
        • 6.4.5 All Numbers
      • 6.5 Variations of Futures
        • 6.5.1 A Lazy Future
        • 6.5.2 Execution on Another Thread
      • 6.6 Modification and Generalization of a Generator
        • 6.6.1 Modifications
        • 6.6.2 Generalization
      • 6.7 Various Job Workflows
        • 6.7.1 The Transparent Awaiter Workflow
        • 6.7.2 Automatically Resuming the Awaiter
        • 6.7.3 Automatically Resuming the Awaiter on a Separate Thread
    • 7. The Future: C++23
      • 7.1 Executors
        • 7.1.1 A long Way
        • 7.1.2 What is an Executor?
        • 7.1.3 First Examples
        • 7.1.4 Goals of an Executor Concept
        • 7.1.5 Terminology
        • 7.1.6 Execution Functions
        • 7.1.7 A Prototype Implementation
      • 7.2 Extended Futures
        • 7.2.1 Concurrency TS v1
        • 7.2.2 Unified Futures
      • 7.3 Transactional Memory
        • 7.3.1 ACI(D)
        • 7.3.2 Synchronized and Atomic Blocks
        • 7.3.3 transaction_safe versus transaction_unsafe Code
      • 7.4 Task Blocks
        • 7.4.1 Fork and Join
        • 7.4.2 define_task_block versus define_task_block_restore_thread
        • 7.4.3 The Interface
        • 7.4.4 The Scheduler
      • 7.5 Data-Parallel Vector Library
        • 7.5.1 Data-Parallel Vectors
        • 7.5.2 The Interface of the Data-Parallel Vectors
  • Patterns
    • 8. Patterns and Best Practices
      • 8.1 History
      • 8.2 Invaluable Value
      • 8.3 Pattern versus Best Practices
      • 8.4 Anti-Pattern
    • 9. Synchronisation Patterns
      • 9.1 Dealing with Sharing
        • 9.1.1 Copied Value
        • 9.1.2 Thread-Specific Storage
        • 9.1.3 Future
      • 9.2 Dealing with Mutation
        • 9.2.1 Scoped Locking
        • 9.2.2 Strategized Locking
        • 9.2.3 Thread-Safe Interface
        • 9.2.4 Guarded Suspension
    • 10. Concurrent Architecture
      • 10.1 Active Object
        • 10.1.1 Components
        • 10.1.2 Advantages and Disadvantages
        • 10.1.3 Implementation
        • 10.1.4 Further Information
      • 10.2 Monitor Object
        • 10.2.1 Requirements
        • 10.2.2 Components
        • 10.2.3 Runtime behavior
        • 10.2.4 Advantages and Disadvantages
        • 10.2.5 Further Information
      • 10.3 Half-Sync/Half-Async
        • 10.3.1 Advantages and Disadvantages
        • 10.3.2 Reactor
        • 10.3.3 Proactor
        • 10.3.4 Further Information
    • 11. Best Practices
      • 11.1 General
        • 11.1.1 Code Reviews
        • 11.1.2 Minimise Sharing of Mutable Data
        • 11.1.3 Minimise Waiting
        • 11.1.4 Prefer Immutable Data
        • 11.1.5 Use pure functions
        • 11.1.6 Look for the Right Abstraction
        • 11.1.7 Use Static Code Analysis Tools
        • 11.1.8 Use Dynamic Enforcement Tools
      • 11.2 Multithreading
        • 11.2.1 Threads
        • 11.2.2 Data Sharing
        • 11.2.3 Condition Variables
        • 11.2.4 Promises and Futures
      • 11.3 Memory Model
        • 11.3.1 Don’t use volatile for synchronization
        • 11.3.2 Don’t program Lock Free
        • 11.3.3 If you program Lock-Free, use well-established patterns
        • 11.3.4 Don’t build your abstraction, use guarantees of the language
        • 11.3.5 Don’t reinvent the wheel
  • Data Structures
    • 12. Lock-Based Data Structures
      • 12.1 General Considerations
        • 12.1.1 Locking Strategy
        • 12.1.2 Granularity of the Interface
        • 12.1.3 Typical Usage Pattern
        • 12.1.4 Avoidance of Loopholes
        • 12.1.5 Contention
        • 12.1.6 Scalability
        • 12.1.7 Invariants
        • 12.1.8 Exceptions
      • 12.2 Concurrent Stack
        • 12.2.1 A Simplified Implementation
        • 12.2.2 A Complete Implementation
      • 12.3 Concurrent Queue
        • 12.3.1 Coarse-Grained Locking
        • 12.3.2 Fine-Grained Locking
  • Further Information
    • 13. Challenges
      • 13.1 ABA Problem
      • 13.2 Blocking Issues
      • 13.3 Breaking of Program Invariants
      • 13.4 Data Races
      • 13.5 Deadlocks
      • 13.6 False Sharing
      • 13.7 Lifetime Issues of Variables
      • 13.8 Moving Threads
      • 13.9 Race Conditions
    • 14. The Time Library
      • 14.1 The Interplay of Time Point, Time Duration, and Clock
      • 14.2 Time Point
        • 14.2.1 From Time Point to Calendar Time
        • 14.2.2 Cross the valid Time Range
      • 14.3 Time Duration
        • 14.3.1 Calculations
      • 14.4 Clocks
        • 14.4.1 Accuracy and Steadiness
        • 14.4.2 Epoch
      • 14.5 Sleep and Wait
    • 15. CppMem - An Overview
      • 15.1 The simplified Overview
        • 15.1.1 1. Model
        • 15.1.2 2. Program
        • 15.1.3 3. Display Relations
        • 15.1.4 4. Display Layout
        • 15.1.5 5. Model Predicates
        • 15.1.6 The Examples
    • 16. Glossary
      • 16.1 adress_free
      • 16.2 ACID
      • 16.3 CAS
      • 16.4 Callable Unit
      • 16.5 Complexity
      • 16.6 Concurrency
      • 16.7 Critical Section
      • 16.8 Deadlock
      • 16.9 Eager Evaluation
      • 16.10 Executor
      • 16.11 Function Objects
      • 16.12 Lambda Functions
      • 16.13 Lazy evaluation
      • 16.14 Lock-free
      • 16.15 Lost Wakeup
      • 16.16 Math Laws
      • 16.17 Memory Location
      • 16.18 Memory Model
      • 16.19 Modification Order
      • 16.20 Monad
      • 16.21 Non-blocking
      • 16.22 Parallelism
      • 16.23 Predicate
      • 16.24 Pattern
      • 16.25 RAII
      • 16.26 Release Sequence
      • 16.27 Sequential Consistency
      • 16.28 Sequence Point
      • 16.29 Spurious Wakeup
      • 16.30 Thread
      • 16.31 Total order
      • 16.32 TriviallyCopyable
      • 16.33 Undefined Behavior
      • 16.34 volatile
      • 16.35 wait-free
    • Index

The Leanpub 45-day 100% Happiness Guarantee

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

See full terms

Do Well. Do Good.

Authors have earned$10,578,526writing, publishing and selling on Leanpub, earning 80% royalties while saving up to 25 million pounds of CO2 and up to 46,000 trees.

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), EPUB (for phones and tablets) and MOBI (for 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, EPUB and/or MOBI files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub