liveBook · Manning

Chapter 10: Chapter 10. Parallel algorithms

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

The introduction connects earlier work on concurrent code and thread pools to the chapter’s focus on C++17 standard parallel algorithms.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction connects earlier work on concurrent code and thread pools to the chapter’s focus on C++17 standard parallel algorithms.

Key terms: C++17 parallel algorithms, parallel algorithms, thread pools

10.1 Parallelizing the standard library algorithms

Introduces C++17 parallel-algorithm overloads, showing how execution policies permit parallel work and change complexity expectations.

Key terms: parallel algorithms, execution policy, overloads, std::execution::par, complexity, parallelism

10.2 Execution policies

Introduces the three standard execution policies, the objects used to pass them to algorithms, and the limits on creating policies.

Key terms: std::execution::sequenced_policy, std::execution::parallel_policy, std::execution::parallel_unsequenced_policy, std::execution::seq, std::execution::par, std::execution::par_unseq, execution policies, implementation-specific behavior

10.2.1 General effects of specifying an execution policy

Specifying an execution policy changes an algorithm’s complexity guarantees, exception behavior, and where and when its steps may run.

Key terms: execution policy, algorithm complexity, O(some-expression), scheduling overhead, std::terminate, std::bad_alloc, std::execution::seq, uncaught exceptions, execution agents, ordering constraints

10.2.2 std::execution::sequenced_policy

The section explains that sequenced execution stays on the calling thread but does not guarantee the operation order of an algorithm call without a policy.

Key terms: std::execution::sequenced_policy, execution policy, calling thread, definite order, unspecified order, std::for_each, iterators, values, callable objects, synchronization mechanisms

10.2.3 std::execution::parallel_policy

This section explains how the parallel execution policy schedules algorithm operations and what callers must do to avoid data races.

Key terms: std::execution::parallel_policy, parallel execution policy, operations, iterators, values, callable objects, data races, ordering between elements, undefined behavior, data race

10.2.4 std::execution::parallel_unsequenced_policy

This section explains the freedom the parallel unsequenced policy gives an algorithm and the resulting restrictions on the operations supplied to it.

Key terms: std::execution::parallel_unsequenced_policy, unordered, unsequenced, threads of execution, iterators, values, callable objects, synchronization, relevant element

10.3 The parallel algorithms from the C++ Standard Library

This section identifies which standard algorithms accept execution policies and explains why their policy overloads require stronger iterator guarantees.

Key terms: execution policy, std::accumulate, std::reduce, associative, commutative, overload, Input Iterator, Output Iterator, Forward Iterator, single-pass

10.3.1 Examples of using parallel algorithms

This section shows how to apply parallel algorithms to independent elements and how execution-policy choices affect synchronization and access to the container.

Key terms: parallel loop, embarrassingly parallel, range, std::execution::par, std::execution::par_unseq, execution policy, internal mutex, per-element mutex, whole-container mutex, external synchronization

10.3.2 Counting visits

This section uses website-log visit counting to show how std::transform_reduce can parse independent entries, combine partial totals, and delegate parallel execution to the Standard Library.

Key terms: log entries, parse_log_line, aggregate, parallel algorithms, count_visits_per_page, std::transform_reduce, reduction operation, combine_visits, log_info, map

10.4 Summary

The chapter recap connects using C++ Standard Library parallel algorithms with choosing execution policies and applying an algorithm in real code.

Concepts explained in this chapter

← Chapter 9: Chapter 9. Advanced thread management Chapter 11: Chapter 11. Testing and debugging multithreaded applications →

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.