liveBook · Manning

Concurrent access

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

Concurrent access is access to the same data by multiple threads during overlapping periods of execution.

Concurrent access occurs when multiple threads use the same data during overlapping execution. A thread-safe data structure preserves its invariants and avoids lost or corrupted data, but safety alone does not guarantee that threads can work on the data at the same time: a mutex can serialize access by making threads take turns. section 6.1

Designing for concurrent access means considering both safety and the opportunity for threads to proceed concurrently:

  • Preserve invariants, avoid races in the interface by providing complete operations, consider exception behavior, and restrict lock scope to reduce deadlock risks. section 6.1.1
  • Ask which operations may overlap, whether locks can cover smaller regions, and whether different parts of the data structure can use different mutexes. section 6.1.1

Shared data and data structures

Sharing an apartment’s kitchen or bathroom illustrates how shared resources need rules for access. In C++, stacks, queues, hash maps, and linked lists can be designed with locks to protect data; the design determines where access must take turns and where threads may overlap. section 3.0 section 6.4

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

How deep it goes
Defines thread safety and serialization, then gives design questions; defers detailed implementations to particular data structures.
Examples
Uses an apartment-sharing analogy and lock-protected stacks, queues, hash maps, and linked lists.
What it assumes
Assumes thread management is already in place and treats shared-data safety as a concern of multithreaded programs.
What you can do after
Evaluate a data structure for race risks and concurrency opportunities by considering lock scope and which operations may overlap.
How it connects
Connects concurrent access to thread safety, mutexes, serialization, race conditions, deadlock, and shared-data design.

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.