liveBook · Manning

Parallel algorithm

As explained in C++ Concurrency in Action, Second Edition

An algorithm that can distribute its work across multiple execution agents.

A parallel algorithm distributes work across multiple execution agents. In C++17, many Standard Library algorithms have overloads that accept an execution policy. The policy specifies how the call may run: `std::execution::par` permits parallel execution but does not require the library to use multiple threads. section 10.1

std::vector<int> my_data;
std::sort(std::execution::par,my_data.begin(),my_data.end());


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":1,\"ch\":10},{\"line\":1,\"ch\":29}]]"}
!@%STYLE%@!

This call sorts a vector with a policy that permits parallel execution. section 10.1

Choosing a policy can change algorithm behavior, complexity requirements, and restrictions on the code. Exception safety also needs care: exceptions from work running on other threads cannot simply propagate through the caller’s stack. Custom parallel algorithms such as `parallel_accumulate` illustrate how futures and thread-joining safeguards can address these risks. section 10.4 section 8.4.1

How C++ Concurrency in Action, Second Edition covers it

How deep it goes
Explains policy behavior and restrictions, and examines exception safety through custom parallel implementations.
Examples
Uses parallel std::sort and custom parallel versions of std::for_each and std::accumulate.
What you can do after
Apply C++ Standard Library execution policies to algorithms while accounting for their behavior and restrictions.
How it connects
Connects execution policies with Standard Library algorithms, threads, futures, and std::async.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of C++ Concurrency in Action, Second Edition. The section links point to the parts of the book it draws on.