liveBook · Manning

Thread safety

As explained in Learn Go with Pocket-Sized Projects

Thread safety is the property that permits concurrent execution by multiple threads without causing incorrect behavior or data races.

Thread safety means allowing multiple concurrent execution paths to access and modify a shared resource without incorrect behavior or data races. The question becomes practical when a cache receives simultaneous operations. section 7.1.3

A sequential test cannot establish thread safety because its ordered operations make the result predictable. A meaningful check must simulate simultaneous calls, such as several goroutines updating a cache during overlapping requests. section 7.3

A shared cache

Consider a generic cache with Read, Upsert, and Delete operations over a map. Two concurrent Upsert calls targeting the same key create the central safety question: how can the design show that shared access will not break the cache? Testing can expose bugs, but cannot prove their absence; synchronization, including mutexes when necessary, must be considered in the design. section 7.3 section 8.7

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Explains shared-resource hazards and concurrent testing, then points to mutexes without detailing synchronization implementation.
Examples
Uses a map-backed generic cache, simultaneous cache updates, and a water-bottle analogy for conflicting shared access.
What it assumes
Assumes familiarity with cache operations and introduces concurrent access through Go goroutines.
What you can do after
Diagnose/debug with it by testing concurrent cache calls and deciding whether synchronization is required.
How it connects
Connects thread safety with goroutines, concurrency, testing, shared caches, and mutexes.

Also explained in

Compare how 8 books explain thread safety →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Go with Pocket-Sized Projects. The section links point to the parts of the book it draws on.