Protecting shared data with a lock
In the Goroutines lesson, every goroutine wrote its result to its own place in a slice. But sometimes many goroutines must change the same variable, like a counter of visitors. This program starts 100 goroutines, and each adds 1 to count a thousand times:
package main
import (
"fmt"
"sync"
)
func main() {
count := 0
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < 1000; j++ {
count++
}
}()
}
wg.Wait()
fmt.Println(count)
}
It should print 100000. When we ran it, it printed 89206, then 82071, then 84332. The number changed from run to run, and once it even printed the right answer, 100000. A bug that only shows up sometimes is very hard to find.
Why updates get lost
count++ looks like one step, but the computer does it in three: read count, add 1, write the new value back. Two goroutines can run at the same time on different parts of the processor (CPU):
| Goroutine A | Goroutine B | count |
|---|---|---|
| reads 7 | 7 | |
| reads 7 | 7 | |
| writes 8 | 8 | |
| writes 8 | 8 |
Both added 1, but count grew by only 1. One update was lost. When goroutines use the same variable at the same time and at least one of them changes it, that is called a data race.
Locking with a mutex
A mutex (short for "mutual exclusion") is a lock. Only one goroutine can hold it at a time. sync.Mutex has two methods:
mu.Lock()takes the lock. If another goroutine holds it, this call waits.mu.Unlock()gives the lock back.
The code between Lock and Unlock runs in only one goroutine at a time:
count := 0
var mu sync.Mutex
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < 1000; j++ {
mu.Lock()
count++
mu.Unlock()
}
}()
}
wg.Wait()
fmt.Println(count)
Output:
100000
Now the read, add and write of count++ always happen together, so no update is lost. The result is 100000 on every run.
Unlock with defer
Often the mutex sits in a struct, next to the data it protects. A method locks it and uses defer to unlock it:
package main
import (
"fmt"
"sync"
)
type Counter struct {
mu sync.Mutex
n int
}
func (c *Counter) Add(x int) {
c.mu.Lock()
defer c.mu.Unlock()
c.n += x
}
func main() {
var c Counter
var wg sync.WaitGroup
for i := 1; i <= 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
c.Add(i)
}()
}
wg.Wait()
fmt.Println(c.n)
}
Output:
55
defer c.mu.Unlock() runs when Add returns, so the lock is always given back, even if the method gets longer later and has several return lines. The method has a pointer receiver, *Counter. Like a WaitGroup, a mutex must not be copied, or each copy would be a different lock.
Forgetting to unlock
If a goroutine takes the lock and never gives it back, the next Lock waits forever:
package main
import (
"fmt"
"sync"
)
func main() {
var mu sync.Mutex
mu.Lock()
fmt.Println("locked once")
mu.Lock()
fmt.Println("never printed")
}
Output:
locked once
fatal error: all goroutines are asleep - deadlock!
Keep the locked part short: lock, change the shared data, unlock. Use a channel when goroutines pass values to each other, and a mutex when they share one variable.
In the exercise, many workers add to one shared total. Protect it with the mutex so the program prints the same correct total every time.