liveBook · Manning

Shared_mutex

As explained in Object-Oriented Software Design in C++

A C++ standard library synchronization class that supports either exclusive ownership by one thread or shared ownership by multiple threads.

A shared_mutex protects a shared resource when readers may run together but writers require exclusive access. A unique_lock blocks every other reader and writer; a shared_lock permits multiple readers but excludes writers. section 16.2.2

Technicians and loggers

Several technician threads set a shared meter, while logger threads read it. A unique lock prevents logging during an update; shared locks let multiple loggers read together. The program creates and joins both thread groups. section 16.2.2

Figure 16.6 Three technicians simultaneously attempt to set the meter’s value; meanwhile, three loggers attempt to read and log the meter’s current setting. Therefore, the meter is the shared resource. Only one technician should be setting the meter at a time, and no logger should read the meter while a technician is setting it. However, unless a technician is setting the meter, multiple loggers can be reading it at the same time.

The meter diagram makes the exclusivity rule concrete: one technician writes, or multiple loggers read, but never both simultaneously. section 16.2.2

How Object-Oriented Software Design in C++ covers it

How deep it goes
Explains exclusive and shared ownership, then demonstrates both modes in meter access; it stops short of deeper locking internals.
Examples
Uses a shared meter updated by technician threads and read by logger threads.
What it assumes
Assumes threads, mutexes, shared resources, and critical regions while introducing the reader-writer access distinction.
What you can do after
Apply it via unique_lock and shared_lock to coordinate concurrent reads and exclusive writes.
How it connects
Connects mutexes, unique_lock, shared_lock, critical regions, threads, and the reader-writer problem.

Also explained in

Compare how 2 books explain shared_mutex →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Object-Oriented Software Design in C++. The section links point to the parts of the book it draws on.