Chapter 1 · Section 3 of 8 · 3 min read
The idea: mutual exclusion
Mutual exclusion in one picture
Think of a room with one key hanging on a hook outside. To go in, you take the key. While you hold it, nobody else can go in. When you leave, you hang the key back. Whoever is waiting takes it next.
- The room is the critical section.
- The key is the mutex.
- Lock is taking the key (or waiting for it).
- Unlock is hanging it back.
A mutex protects data, not code. Which data belongs to which mutex is a promise that you, the programmer, keep. The Go convention is to put the mutex next to the data it guards and say so in a comment, as Counter in this chapter does:
type Counter struct {
mu sync.Mutex // guards n
n int
}Every read and every write of n must happen with mu held. If one place forgets, the protection is gone for all of them.
What goes inside the critical section
Put in everything that must look like one step to the rest of the program. In the bank account, that is the check and the change. Put in as little else as you can, because everyone else waits while you are inside.
A useful way to decide: write down the invariant, the fact that must always be true when nobody is inside. For the account it is "the balance is never negative". Inside the critical section the invariant may be briefly false. Outside it must always be true. The mutex is what keeps other goroutines from looking while it is false.
Waiting and queues
If the lock is taken, you wait. Waiting has two possible shapes:
- Sleep. You tell the system "wake me when it is free" and stop using the processor. That is cheap for long waits and slower to resume.
- Spin. You loop, asking again and again. That is fast to resume and wastes processor time.
Real mutexes mix the two, and so does Go's, as you will see in the source. When several goroutines wait, they usually queue up.
Who goes next
When the lock is released and several goroutines are waiting, someone must choose who takes it. That is a policy, and the choice has consequences:
- Hand it to the one who waited longest. That is fair, and it avoids starving anyone, but it can cost speed.
- Let whoever is quickest grab it. That is usually faster overall, but a goroutine can lose again and again.
You will see both policies in Go's mutex, and a simulator that shows the difference.
What Unlock promises
A mutex does more than keep goroutines apart. It also controls what they can see. The Go documentation puts it in the terms of its memory model:
In the terminology of the Go memory model, the n'th call to Mutex.Unlock "synchronizes before" the m'th call to Mutex.Lock for any n < m.
Source:
go doc sync.Mutex, Go 1.25.2.
In plain words: everything a goroutine wrote before it unlocked is visible to the next goroutine that locks. Without that promise, a goroutine could take the lock and still read a stale value. Chapter 33 explains "synchronizes before" and the memory model properly. For now, remember that a mutex orders accesses as well as separating them.
Who may unlock
In Go, the mutex does not remember who locked it. The documentation for Unlock says so:
A locked Mutex is not associated with a particular goroutine. It is allowed for one goroutine to lock a Mutex and then arrange for another goroutine to unlock it.
Source:
sync/mutex.go, comment onUnlock, Go 1.25.2.
That is a design choice. The Linux kernel, as you will see, makes the opposite one.
Summary so far
- A critical section is code that must run alone.
- A mutex lets one goroutine at a time in.
- It protects data, and the rule is a convention you keep.
- It needs a policy for who goes next.
- Unlock also makes your writes visible to the next locker.