liveBook · Manning

Rwmutex

As explained in Go by Example

RWMutex is a Go synchronization type that permits concurrent read access while allowing only one writer at a time.

RWMutex protects shared state when reads outnumber writes: multiple goroutines may read concurrently, while a write obtains exclusive access. This limits race conditions without serializing every read. section 8.2.4

Protecting an in-memory map

Embed sync.RWMutex beside the shared map. A write calls Lock and defers Unlock; a read calls RLock and defers RUnlock. The Shortener can then safely serve concurrent Resolve calls while serializing Shorten operations.

How Go by Example covers it

How deep it goes
Defines read and write access modes, demonstrates map protection, and defers performance choice to benchmarking.
Examples
Uses a concurrent link shortener whose in-memory map supports Shorten writes and Resolve reads.
What it assumes
Assumes goroutines and shared state, while reviewing mutex locking and unlocking before introducing read-write locking.
What you can do after
Apply it via Go's sync.RWMutex to protect shared maps and other read-heavy state from unsafe concurrent use.
How it connects
Connects mutexes with goroutines, shared state, race conditions, maps, and read-write throughput.

Also explained in

Compare how 4 books explain rwmutex →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go by Example. The section links point to the parts of the book it draws on.