July 2019
Intermediate to advanced
458 pages
12h 12m
English
Some care must be taken when locking and unlocking mutexes in order to avoid unexpected behavior and deadlocks in the application. Take the following snippet:
for condition { mu.Lock() defer mu.Unlock() action()}
This code seems okay at first sight, but it will inevitably block the goroutine. This is because the defer statement is not executed at the end of each loop iteration, but when the function returns. So the first attempt will lock without releasing and the second attempt will remain stuck.
A little refactor can help fix this, as shown in the following snippet:
for condition { func() { mu.Lock() defer mu.Unlock() action() }()}
We can use a closure to be sure that the deferred Unlock gets executed, even if action panics. ...
Read now
Unlock full access