liveBook · Manning

Thread safety

As explained in Grokking Concurrency

Thread safety is the property of a function or operation behaving correctly when accessed concurrently by multiple tasks under any scheduling or interleaving.

Imagine two tasks sharing one printer: without controlled access, their actions can conflict and leave the application—or the whole system—in an unknown state. Thread safety means a function or operation still behaves correctly when multiple tasks access it, whatever scheduling or interleaving occurs. section 8.1

The safest design usually shares as little as possible and minimizes communication between tasks. When sharing cannot be avoided, synchronized access must prevent operations from alternating in ways that produce incorrect results. section 8.1 section 8.2

Practical safeguards include:

  • Prefer immutable objects and pure functions when they fit; unchanged state cannot be corrupted by thread interference or observed inconsistently. section 8.1
  • Check whether libraries are thread safe before concurrent use; synchronize their calls or abandon libraries whose global state cannot support concurrent access. section 8.2
  • Consider message-passing IPC instead of shared memory so tasks work with their own data copies. section 9.10

A bank account race

Suppose many ATM tasks each deposit 10 and then withdraw 10. An unsynchronized account can finish with a balance of 380 instead of the expected 0 because concurrent reads and writes interfere. Timing changes, such as inserting sleeps, may help expose the problem, but synchronization is needed to prevent the incorrect interleaving. section 8.2

Synchronization improves correctness but costs performance: locks introduce delays, require processor and synchronization work, and can limit scalability when many tasks contend for one lock. section 9.10

How Grokking Concurrency covers it

How deep it goes
Defines correctness, demonstrates race conditions, and weighs synchronization costs without developing immutable or pure-function methods.
Examples
Uses a shared printer, an unsynchronized bank account, concurrent ATMs, timing delays, and incorrect account balances.
What you can do after
Diagnose possible race conditions by perturbing timing and protect unsafe shared access through synchronization or reduced sharing.
How it connects
Connects thread safety with shared resources, race conditions, synchronization, immutability, pure functions, locks, and message-passing IPC.

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 Grokking Concurrency. The section links point to the parts of the book it draws on.