Mastering C++ Multithreading

Book description

Master multithreading and concurrent processing with C++

About This Book

  • Delve into the fundamentals of multithreading and concurrency and find out how to implement them
  • Explore atomic operations to optimize code performance
  • Apply concurrency to both distributed computing and GPGPU processing

Who This Book Is For

This book is for intermediate C++ developers who wish to extend their knowledge of multithreading and concurrent processing. You should have basic experience with multithreading and be comfortable using C++ development toolchains on the command line.

What You Will Learn

  • Deep dive into the details of the how various operating systems currently implement multithreading
  • Choose the best multithreading APIs when designing a new application
  • Explore the use of mutexes, spin-locks, and other synchronization concepts and see how to safely pass data between threads
  • Understand the level of API support provided by various C++ toolchains
  • Resolve common issues in multithreaded code and recognize common pitfalls using tools such as Memcheck, CacheGrind, DRD, Helgrind, and more
  • Discover the nature of atomic operations and understand how they can be useful in optimizing code
  • Implement a multithreaded application in a distributed computing environment
  • Design a C++-based GPGPU application that employs multithreading

In Detail

Multithreaded applications execute multiple threads in a single processor environment, allowing developers achieve concurrency. This book will teach you the finer points of multithreading and concurrency concepts and how to apply them efficiently in C++.

Divided into three modules, we start with a brief introduction to the fundamentals of multithreading and concurrency concepts. We then take an in-depth look at how these concepts work at the hardware-level as well as how both operating systems and frameworks use these low-level functions.

In the next module, you will learn about the native multithreading and concurrency support available in C++ since the 2011 revision, synchronization and communication between threads, debugging concurrent C++ applications, and the best programming practices in C++.

In the final module, you will learn about atomic operations before moving on to apply concurrency to distributed and GPGPU-based processing. The comprehensive coverage of essential multithreading concepts means you will be able to efficiently apply multithreading concepts while coding in C++.

Style and approach

This book is filled with examples that will help you become a master at writing robust concurrent and parallel applications in C++.

Publisher resources

Download Example Code

Table of contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
      1. Downloading the example code
      2. Errata
      3. Piracy
      4. Questions
  2. Revisiting Multithreading
    1. Getting started
    2. The multithreaded application
      1. Makefile
    3. Other applications
    4. Summary
  3. Multithreading Implementation on the Processor and OS
    1. Defining processes and threads
      1. Tasks in x86 (32-bit and 64-bit)
      2. Process state in ARM
    2. The stack
    3. Defining multithreading
      1. Flynn's taxonomy
      2. Symmetric versus asymmetric multiprocessing
      3. Loosely and tightly coupled multiprocessing
      4. Combining multiprocessing with multithreading
      5. Multithreading types
        1. Temporal multithreading
        2. Simultaneous multithreading (SMT)
    4. Schedulers
    5. Tracing the demo application
    6. Mutual exclusion implementations
      1. Hardware
      2. Software
    7. Summary
  4. C++ Multithreading APIs
    1. API overview
    2. POSIX threads
      1. Windows support
      2. PThreads thread management
      3. Mutexes
      4. Condition variables
      5. Synchronization
      6. Semaphores
      7. Thread local storage (TLC)
    3. Windows threads
      1. Thread management
      2. Advanced management
      3. Synchronization
      4. Condition variables
      5. Thread local storage
    4. Boost
    5. Qt
      1. QThread
      2. Thread pools
      3. Synchronization
      4. QtConcurrent
      5. Thread local storage
    6. POCO
      1. Thread class
      2. Thread pool
      3. Thread local storage (TLS)
      4. Synchronization
    7. C++ threads
    8. Putting it together
    9. Summary
  5. Thread Synchronization and Communication
    1. Safety first
    2. The scheduler
      1. High-level view
      2. Implementation
        1. Request class
        2. Worker class
      3. Dispatcher
      4. Makefile
      5. Output
    3. Sharing data
      1. Using r/w-locks
      2. Using shared pointers
    4. Summary
  6. Native C++ Threads and Primitives
    1. The STL threading API
      1. Boost.Thread API
    2. The 2011 standard
    3. C++14
    4. C++17
    5. STL organization
    6. Thread class
      1. Basic use
      2. Passing parameters
      3. Return value
      4. Moving threads
      5. Thread ID
      6. Sleeping
      7. Yield
      8. Detach
      9. Swap
    7. Mutex
      1. Basic use
        1. Non-blocking locking
      2. Timed mutex
      3. Lock guard
      4. Unique lock
      5. Scoped lock
      6. Recursive mutex
      7. Recursive timed mutex
    8. Shared mutex
      1. Shared timed mutex
    9. Condition variable
      1. Condition_variable_any
      2. Notify all at thread exit
    10. Future
      1. Promise
        1. Shared future
      2. Packaged_task
      3. Async
        1. Launch policy
    11. Atomics
    12. Summary
  7. Debugging Multithreaded Code
    1. When to start debugging
    2. The humble debugger
      1. GDB
      2. Debugging multithreaded code
      3. Breakpoints
      4. Back traces
    3. Dynamic analysis tools
      1. Limitations
      2. Alternatives
      3. Memcheck
        1. Basic use
        2. Error types
          1. Illegal read / illegal write errors
          2. Use of uninitialized values
          3. Uninitialized or unaddressable system call values
          4. Illegal frees
          5. Mismatched deallocation
          6. Overlapping source and destination
          7. Fishy argument values
          8. Memory leak detection
      4. Helgrind
        1. Basic use
      5. Misuse of the pthreads API
      6. Lock order problems
      7. Data races
      8. DRD
      9. Basic use
      10. Features
      11. C++11 threads support
    4. Summary
  8. Best Practices
    1. Proper multithreading
    2. Wrongful expectations - deadlocks
    3. Being careless - data races
    4. Mutexes aren't magic
    5. Locks are fancy mutexes
    6. Threads versus the future
    7. Static order of initialization
    8. Summary
  9. Atomic Operations - Working with the Hardware
    1. Atomic operations
      1. Visual C++
      2. GCC
        1. Memory order
      3. Other compilers
      4. C++11 atomics
      5. Example
      6. Non-class functions
      7. Example
      8. Atomic flag
      9. Memory order
        1. Relaxed ordering
        2. Release-acquire ordering
        3. Release-consume ordering
        4. Sequentially-consistent ordering
        5. Volatile keyword
    2. Summary
  10. Multithreading with Distributed Computing
    1. Distributed computing, in a nutshell
      1. MPI
        1. Implementations
        2. Using MPI
      2. Compiling MPI applications
      3. The cluster hardware
    2. Installing Open MPI
      1. Linux and BSDs
      2. Windows
    3. Distributing jobs across nodes
      1. Setting up an MPI node
      2. Creating the MPI host file
      3. Running the job
      4. Using a cluster scheduler
    4. MPI communication
      1. MPI data types
        1. Custom types
      2. Basic communication
      3. Advanced communication
      4. Broadcasting
      5. Scattering and gathering
    5. MPI versus threads
    6. Potential issues
    7. Summary
  11. Multithreading with GPGPU
    1. The GPGPU processing model
      1. Implementations
      2. OpenCL
      3. Common OpenCL applications
      4. OpenCL versions
        1. OpenCL 1.0
        2. OpenCL 1.1
        3. OpenCL 1.2
        4. OpenCL 2.0
        5. OpenCL 2.1
        6. OpenCL 2.2
    2. Setting up a development environment
      1. Linux
      2. Windows
      3. OS X/MacOS
    3. A basic OpenCL application
    4. GPU memory management
    5. GPGPU and multithreading
      1. Latency
    6. Potential issues
    7. Debugging GPGPU applications
    8. Summary

Product information

  • Title: Mastering C++ Multithreading
  • Author(s): Maya Posch
  • Release date: July 2017
  • Publisher(s): Packt Publishing
  • ISBN: 9781787121706