Mastering C++ Multithreading

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

A comprehensive guide to developing effective multithreading applications in 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 processingWho This Book Is ForThis 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 multithreadingIn DetailMultithreaded 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++.

Author(s): Maya Posch
Year: 2017

Language: English
Pages: 461

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