liveBook · Manning

Chapter 2: Chapter 2. Managing threads

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

The chapter introduces std::thread as the main way to manage threads and previews the thread-management tasks it will cover.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduces std::thread as the main way to manage threads and previews the thread-management tasks it will cover.

Key terms: std::thread, launching a thread, waiting for a thread to finish, running threads in the background, parameters, thread function, ownership, number of threads, identifying threads

2.1 Basic thread management

Introduces the main thread, the lifecycle of additional threads, and the need to launch a thread before waiting for it.

Key terms: thread, main(), entry point, std::thread, wait

2.1.1 Launching a thread

This section shows how to launch a C++ thread and why its ownership and access to local data must be handled before the launching function exits.

Key terms: std::thread, <thread>, callable, function object, function call operator, most vexing parse, uniform initialization, lambda expression, join, detach

2.1.2 Waiting for a thread to complete

This section explains how joining a std::thread waits for its completion, what happens to the thread object afterward, and when other waiting mechanisms are needed.

Key terms: std::thread, join(), local variables, joinable(), condition variables, futures

2.1.3 Waiting in exceptional circumstances

This section shows how to ensure a thread is joined on both normal and exceptional exits, and how an RAII guard makes that guarantee less error-prone.

Key terms: join(), std::thread, try/catch, std::terminate(), RAII, thread_guard, destructor, joinable(), =delete, detach()

2.1.4 Running threads in the background

This subsection explains how detaching a std::thread lets it run independently, when detachment is valid, and why a document-editing application might use it.

Key terms: detach(), std::thread, joinable, C++ Runtime Library, joinable(), associated thread of execution, daemon threads, fire-and-forget, edit_document, std::thread constructor

2.2 Passing arguments to a thread function

This section explains how std::thread passes arguments to a new thread, including copies, references, member functions, and move-only values.

Key terms: std::thread constructor, internal storage, rvalues, implicit conversion, buffer, std::string, undefined behavior, non-const reference, std::ref, rvalue

2.3 Transferring ownership of a thread

This section shows how moving std::thread objects transfers responsibility for a thread, enabling functions, scope-bound wrappers, and containers to manage when threads finish.

Key terms: std::thread, std::move, ownership, temporary, move assignment, std::terminate(), join, detach, scoped_thread, thread_guard

2.4 Choosing the number of threads at runtime

The section uses a naïve parallel accumulate to show how to choose a thread count at runtime, divide work, collect results, and recognize the limits of the approach.

Key terms: std::thread::hardware_concurrency(), minimum block size, oversubscription, parallel_accumulate, block, std::thread, std::vector<T>, std::vector<std::thread>, std::for_each, std::accumulate

2.5 Identifying threads

This section explains how to obtain, compare, and use C++ thread identifiers, while treating their values as opaque.

Key terms: std::thread::id, get_id(), std::this_thread::get_id(), default-constructed std::thread::id, comparison operators, total ordering, std::hash<std::thread::id>, associative containers, unordered associative containers, thread-local storage

2.6 Summary

The chapter recap brings together the basics of managing C++ threads and explains why later chapters turn to shared data and synchronization.

Where this is used later

Concepts explained in this chapter

← Chapter 1: Chapter 1. Hello, world of concurrency in C++! Chapter 3: Chapter 3. Sharing data between threads →

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.