liveBook · Manning

Chapter 11: Chapter 11. Testing and debugging multithreaded applications

What you will learn in chapter 11 of C++ Concurrency in Action, Second Edition

The chapter introduces the difficulty of testing and debugging concurrent code and outlines an issues-first approach to finding and removing bugs.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduces the difficulty of testing and debugging concurrent code and outlines an issues-first approach to finding and removing bugs.

Key terms: testing, debugging, concurrent code, concurrency-related bugs, code review, multithreaded tests, performance

11.1 Types of concurrency-related bugs

Introduces unwanted blocking and race conditions as the two broad categories of bugs directly related to concurrency.

Key terms: unwanted blocking, race conditions, concurrency-related bugs

11.1.1 Unwanted blocking

This section explains when waiting becomes an unwanted blocking problem and distinguishes deadlock, livelock, and waits for external input.

Key terms: blocked, unwanted blocking, deadlock, livelock, spin lock, I/O, external input

11.1.2 Race conditions

The section distinguishes harmless scheduling-dependent behavior from harmful race conditions, describes three common kinds of race-related bugs, and explains why some are hard to locate.

Key terms: race condition, data race, undefined behavior, atomic operations, mutex, broken invariants, temporal, partial updates, double-free, lifetime issues

11.2 Techniques for locating concurrency-related bugs

This section introduces careful code review and multithreaded testing as complementary ways to find concurrency-related bugs.

Key terms: reviewing code, concurrency issues, nonconcurrency issues, testing multithreaded code, reviewing the code

11.2.1 Reviewing code to locate potential bugs

This section shows how deliberate code review can uncover subtle concurrency bugs by questioning data protection, thread interleavings, and assumptions about shared data.

Key terms: code review, reviewer, concurrent access, mutexes, ordering requirements, valid, corner case, mutex, public data, pointer

11.2.2 Locating concurrency-related bugs by testing

The section explains why concurrency bugs are hard to reproduce and how focused tests, single-thread comparisons, varied operation scenarios, and test-environment choices help locate them.

Key terms: thread scheduling, race condition, indeterminate, concurrent queue, single thread, worker threads, push(), pop(), empty queue, full queue

11.2.3 Designing for testability

Design multithreaded code so most logic can be tested without concurrency, leaving a smaller, more manageable set of thread interactions to test.

Key terms: designing for testability, test harness, communication paths between threads, state logic, message routing code, read shared data/transform data/update shared data, internal variables, shared data, protection and synchronization

11.2.4 Multithreaded testing techniques

The section compares three ways to expose scheduling-dependent bugs in multithreaded tests: repeated runs, exhaustive simulation, and specialized testing libraries.

Key terms: brute-force testing, stress testing, scheduling permutations, false confidence, combination simulation testing, C++ memory model, race conditions, deadlocks, synchronization primitives, mutexes

11.2.5 Structuring multithreaded test code

This section shows how to structure a multithreaded test so its operations have a good chance of running concurrently and its results can be checked safely.

Key terms: general setup, thread-specific setup, concurrent execution, after completion, std::promise, std::shared_future, ready signal, go signal, std::launch::async, blocking pop()

11.2.6 Testing the performance of multithreaded code

This section explains how to test whether multithreaded code improves performance and scales across machines with different numbers of cores.

Key terms: performance, scalability, serial portion, performance gain, contention, scalability graph, single-processor system

11.3 Summary

The chapter summary recaps the kinds of concurrency bugs covered and the approaches presented for finding and testing them.

Concepts explained in this chapter

← Chapter 10: Chapter 10. Parallel algorithms

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.