liveBook · Manning

Task-based work division

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

Assignment of portions of a computation to different threads for execution.

Task-based work division assigns different responsibilities to different threads. Instead of giving each thread a chunk of data on which to perform the same operation, it makes threads specialists. They may use the same data, but for different purposes. Each thread can focus on one ongoing task, while another thread supplies data or triggers an event it must handle. section 8.1.3

Keeping an application responsive

A single-threaded application might do a little work on task A, then task B, check for key presses and network data, and repeat. Task A must save its state and return to the loop regularly. As more tasks enter the loop, responses can become slow; a long-running task may freeze the interface. Running the tasks in separate threads avoids making each task explicitly yield to that loop. section 8.1.3

Choose the division to fit the work. Task-based division separates concerns; data-based division gives threads portions of a large dataset. For data-heavy operations, inspect access patterns and profile alternatives on the target architecture: changing the division can improve performance without changing the basic algorithm. section 8.1.3 section 8.3.1

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

How deep it goes
Explains task specialization through a single-threaded event-loop comparison, but does not provide a task-assignment implementation.
Examples
Uses key presses, incoming network data, and an unresponsive interface to illustrate competing ongoing tasks.
What it assumes
Introduces the concurrency motivation through a familiar single-threaded loop rather than assuming prior concurrency experience.
What you can do after
Evaluate/understand when distinct threads can keep ongoing tasks separate, and when data-access patterns instead call for profiling data partitions.
How it connects
Connects task specialization to separation of concerns, the single responsibility principle, event handling, and data partitioning.

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.