chapter ten

10 Parallel algorithms

 

This chapter covers

    ·   Using the C++17 parallel algorithms

    In the last chapter we looked at advanced thread management and thread pools, and in chapter 8 we looked at designing concurrent code, using parallel versions of some algorithms as examples. In this chapter, we're going to look at the parallel algorithms provided by the C++17 standard, so let's start, without further ado.

    10.1  Parallelizing the standard library algorithms

    The C++17 standard added the concept of “parallel algorithms” to the C++ Standard Library. These are additional overloads of many of the functions that operate on ranges, such as std::find, std::transform, or std::reduce. The parallel versions have the same signature as the “normal” single-threaded versions, except for the addition of a new first parameter which specifies the execution policy to use. For example,

    10.2  Execution policies

    10.2.1    General effects of specifying an execution policy

    10.2.2    std::execution::sequenced_policy

    10.2.3    std::execution::parallel_policy

    10.2.4    std::execution::parallel_unsequenced_policy

    10.3  The parallel algorithms from the C++ Standard Library

    10.3.1    Examples of using parallel algorithms

    10.3.2    Counting visits

    10.4  Summary