Chapter 1 · Section 2 of 8 · 3 min read

Break it, then lock it

This is the simulator from Chapter 0, with the Use a mutex switch turned on. Each increment is now five steps: lock, read, add 1, write, unlock. A goroutine that wants the lock while someone else holds it shows an amber box and waits in the mutex's queue.

This animation needs JavaScript. Here is a text version of one run.

  1. Start: counter = 0. 4 goroutines, 2 increments each.
  2. G3 locks the mutex.
  3. G1 tries to lock, but G3 holds it. G1 waits.
  4. G3 reads the counter: 0.
  5. G3 adds one: its copy is now 1.
  6. G3 writes 1.
  7. G3 unlocks. The lock goes to G1, which was waiting.
  8. G4 tries to lock, but G1 holds it. G4 waits.
  9. G3 tries to lock, but G1 holds it. G3 waits.
  10. G2 tries to lock, but G1 holds it. G2 waits.
  11. G1 reads the counter: 1.
  12. G1 adds one: its copy is now 2.
  13. G1 writes 2.
  14. G1 unlocks. The lock goes to G4, which was waiting.
  15. G1 tries to lock, but G4 holds it. G1 waits.
  16. G4 reads the counter: 2.
  17. G4 adds one: its copy is now 3.
  18. G4 writes 3.
  19. G4 unlocks. The lock goes to G3, which was waiting.
  20. G3 reads the counter: 3.
  21. G3 adds one: its copy is now 4.
  22. G3 writes 4.
  23. G3 unlocks. The lock goes to G2, which was waiting. G3 is finished.
  24. G2 reads the counter: 4.
  25. G2 adds one: its copy is now 5.
  26. G2 writes 5.
  27. G4 tries to lock, but G2 holds it. G4 waits.
  28. G2 unlocks. The lock goes to G1, which was waiting.
  29. G2 tries to lock, but G1 holds it. G2 waits.
  30. G1 reads the counter: 5.
  31. G1 adds one: its copy is now 6.
  32. G1 writes 6.
  33. G1 unlocks. The lock goes to G4, which was waiting. G1 is finished.
  34. G4 reads the counter: 6.
  35. G4 adds one: its copy is now 7.
  36. G4 writes 7.
  37. G4 unlocks. The lock goes to G2, which was waiting. G4 is finished.
  38. G2 reads the counter: 7.
  39. G2 adds one: its copy is now 8.
  40. G2 writes 8.
  41. G2 unlocks. G2 is finished.

Final counter: 8. Expected: 8. Every update survived.

Four goroutines each add one to a shared counter twice, this time guarded by a mutex. Switching is set high, so the scheduler changes goroutine often. The counter should end at 8.

What you should have noticed

  • With the mutex on, I ran seeds 1 to 200 at 4 goroutines and 3 increments each, and the final count was right every time. The engine's tests check this. The lock does not make the steps happen in a different order. It makes sure that only one goroutine is between its lock and its unlock at any moment.
  • Inside the critical section the goroutines still take turns in some random order. The mutex does not decide who goes first. It only guarantees they never overlap.
  • A paused lock holder stops everybody who needs the lock. That is the price. A mutex turns "wrong answer" into "waiting", and waiting has a cost you will measure later in this chapter.
  • The unlock step hands the lock directly to the first waiter here. Real mutexes sometimes do that and sometimes do not, which is the subject of the fairness part of this chapter.