liveBook · Manning

Recursion

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

Recursion is a technique in which a function or process calls itself to solve smaller instances of a problem.

Recursion solves a problem through calls that handle smaller instances. In Quicksort, partitioning first identifies which elements belong before or after a pivot; each side is then sorted recursively. Because the divisions emerge during processing, they cannot be assigned to threads up front. section 8.1.2 section 8.1.1

After partitioning, the recursive calls sort separate sets of elements, so they can run concurrently. Using std::async for one recursive branch lets the C++ Thread Library choose whether to run it on a new thread or synchronously. Creating a thread at every level can cause excessive thread counts or exhaust available threads. section 8.1.2

To limit thread creation, the recursive workload can be managed in other ways:

  • Choose a thread count with std::thread::hardware_concurrency() and place chunks awaiting sorting on a thread-safe stack. section 8.1.2
  • For accumulate, recursively divide the reduction when there are more threads than the minimum number of items per thread, or let workers perform reduction steps as they finish. section 8.1.1
Figure 8.2. Recursively dividing data

A diagram of recursive data division in Quicksort. section 8.1.2

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

How deep it goes
Explains recursive Quicksort calls and concurrency choices, including thread-count risks and alternatives.
Examples
Uses Quicksort partitioning and recursive accumulate reduction to illustrate recursive work division.
What you can do after
Evaluate recursive tasks for concurrent execution by checking data independence and controlling thread creation.
How it connects
Connects recursion to Quicksort, std::async, thread-safe stacks, and parallel reduction with accumulate.

Also explained in

Compare how 23 books explain recursion →

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.