Helion


Szczegóły ebooka

Concurrency with Modern C++

Concurrency with Modern C++


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. Concurrency with Modern C++ is a practical guide that gets you to grips with concurrent programming in Modern C++.

Starting with the C++ memory model and using many ready-to-run code examples, the book covers everything you need to improve your C++ multithreading skills. You'll gain insight into different design patterns. You'll also uncover the general consideration you have to keep in mind while designing a concurrent data structure. The final chapter in the book talks extensively about the common pitfalls of concurrent programming and ways to overcome these hurdles.

By the end of the book, you'll have the skills to build your own concurrent programs and enhance your knowledge base.

  • Reader Testimonials
  • Introduction
    • Conventions
      • Special Fonts
      • Special Symbols
      • Special Boxes
    • Source Code
      • Run the Programs
    • How you should read the book?
    • Personal Notes
      • Acknowledgements
      • About Me
      • My Special Circumstances
  • 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
    • The Near Future: C++20
      • std::jthread
      • Atomic Smart Pointers
      • Latches and Barriers
      • Semaphores
      • Coroutines
    • Case Studies
      • Calculating the Sum of a Vector
      • Thread-Safe Initialisation of a Singleton
      • Ongoing Optimisation with CppMem
    • The Future: C++23
      • Executors
      • Extended futures
      • Transactional Memory
      • Task Blocks
      • Data-Parallel Vector Library
    • Patterns and Best Practices
      • Synchronisation
      • Concurrent Architecture
      • Best Practices
    • Data Structures
    • Challenges
    • Time Library
    • CppMem
    • Glossary
  • 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
      • The Class Template std::atomic
      • All Atomic Operations
      • Free Atomic Functions
      • std::shared_ptr
    • The Class Template std::atomic_ref (C++20)
      • Specialisations of std::atomic_ref
    • The Synchronisation 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
    • Threads
      • Thread Creation
      • Thread Lifetime
      • Thread Arguments
      • Methods
    • Shared Data
      • Mutexes
      • Locks
      • Thread-safe Initialisation
    • Thread-Local Data
    • Condition Variables
      • The Predicate
      • Lost Wakeup and Spurious Wakeup
      • The Wait Workflow
    • Tasks
      • Tasks versus Threads
      • std::async
      • std::packaged_task
      • std::promise and std::future
      • std::shared_future
      • Exceptions
      • Notifications
  • Parallel Algorithms of the Standard Template Library
    • Execution Policies
      • Parallel and Vectorised Execution
      • Exceptions
      • Hazards of Data Races and Deadlocks
    • Algorithms
    • The New Algorithms
      • More overloads
      • The functional Heritage
    • Performance
  • The Near Future: C++20
    • A Cooperatively Interruptible Joining Thread
      • Automatically Joining
      • Interrupt a std::jthread
      • Stop Tokens
    • Atomic Smart Pointers
      • A thread-safe singly linked list
    • Latches and Barriers
      • std::latch
      • std::barrier
    • Semaphores
    • Coroutines
      • A Generator Function
      • Details
      • The Framework
  • 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
    • Thread-Safe Initialisation 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 Optimisation 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
  • The Future: C++23
    • 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 and Best Practices
    • History
    • Invaluable Value
    • Pattern versus Best Practices
    • Anti-Pattern
  • Synchronisation 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
      • Components
      • Advantages and Disadvantages
      • Implementation
      • Further Information
    • Monitor Object
      • Requirements
      • Components
      • Runtime behaviour
      • Advantages and Disadvantages
      • Further Information
    • Half-Sync/Half-Async
      • Advantages and Disadvantages
      • Reactor
      • Proactor
      • Further Information
  • Best Practices
    • General
      • Code Reviews
      • Minimise Sharing of Mutable Data
      • Minimise 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
      • Dont use volatile for synchronisation
      • Dont program Lock Free
      • If you program Lock-Free, use well-established patterns
      • Dont build your abstraction, use guarantees of the language
      • Dont reinvent the wheel
  • Lock-Based Data Structures
    • General Considerations
      • Locking Strategy
      • Granularity of the Interface
      • Typical Usage Pattern
      • Avoidance of Loopholes
      • Contention
      • Scalability
      • Invariants
      • Exceptions
    • Concurrent Stack
      • A Simplified Implementation
      • A Complete Implementation
    • Concurrent Queue
      • Coarse-Grained Locking
      • Fine-Grained Locking
  • 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
    • ACID
    • CAS
    • Callable Unit
    • Complexity
    • Concurrency
    • Critical Section
    • Eager Evaluation
    • Executor
    • Function Objects
    • Lambda Functions
    • Lazy evaluation
    • Lock-free
    • Lost Wakeup
    • Math Laws
    • Memory Location
    • Memory Model
    • Modification Order
    • Monad
    • Non-blocking
    • Parallelism
    • Predicate
    • Pattern
    • RAII
    • Release Sequence
    • Sequential Consistency
    • Sequence Point
    • Spurious Wakeup
    • Thread
    • Total order
    • TriviallyCopyable
    • volatile
    • wait-free
  • Index