liveBook · Manning

Lock-based data structure

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

A data structure whose concurrent operations use locks to control access to its data.

A lock-based data structure uses mutexes and locks to control concurrent access. Readers may share access while writers require exclusive access; operations can also allow different kinds of work concurrently while serializing identical operations. section 6.1.1

Designing one means balancing safe access with opportunities for concurrency:

  • Lock the right mutex whenever data is accessed, and hold it for as little time as possible. section 6.2
  • Keep data inaccessible outside its lock and prevent races in the interface. section 6.2
  • Using separate mutexes for different parts can increase concurrency, but operations needing multiple locks introduce deadlock risks. section 6.2

A stack with one mutex provides a simple starting point for considering both thread safety and how much concurrency a design permits. Lock-free designs are treated separately. section 6.2 section 6.1.1

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

How deep it goes
Explains mutex selection, lock duration, race risks, and deadlocks, then applies guidelines to simple structures.
Examples
Uses a stack protected by a single mutex as a starting example.
What you can do after
Build lock-based structures by protecting access, minimizing lock duration, and considering concurrency and deadlock risks.
How it connects
Connects mutexes and locks with shared_mutex, race conditions, deadlock, and lock-free data structures.

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.