Part 1 · Synchronization primitives

Chapter 1: Mutex

Let one goroutine at a time into the code that touches shared data. How Go's sync.Mutex does it, how it stays fair, and how Linux, Java, C++, Rust and Python make the same trade-offs.

8 sections · about 33 minutes to read · Go 1.25.2

  1. Taking turnsA bank account that goes negative, and what we need to stop it.2 min
  2. Break it, then lock itTurn a mutex on in the simulator and see what it fixes and what it costs.3 min
  3. The idea: mutual exclusionCritical sections, ownership, waiting, and what Unlock promises.3 min
  4. In Go: reading sync.MutexThe real source: fast path, spinning, queueing, normal and starvation mode.11 min
  5. In the wildGo, the Linux kernel and Java's ReentrantLock compared, with what their own documents say.6 min
  6. Other languagesstd::mutex, Rust's Mutex, Python's Lock and Java's ReentrantLock side by side.4 min
  7. PitfallsForgotten unlocks, copied mutexes, self-deadlock and locks that guard too little.2 min
  8. Check yourselfQuestions, exercises, and what comes next.2 min