I'm reading The Little Go Book.
Page 76 demonstrates how you can deadlock with a single lock:
var (
lock sync.Mutex
)
func main() {
go func() { lock.Lock() }()
time.Sleep(time.Millisecond * 10)
lock.Lock()
}
Running this results in a deadlock as explained by the author. However, what I don't understand is why.
I changed the program to this:
var (
lock sync.Mutex
)
func main() {
go func() { lock.Lock() }()
lock.Lock()
}
My expectation was that a deadlock would still be thrown. But it wasn't.
Could someone explain to me what's happening here please?
The only scenario I can think of that explains this is the following (but this is guesswork):
First example
- The lock is acquired in the first goroutine
- The call to
time.Sleep()ensures the lock is acquired - The
mainfunction attempts to acquire the lock resulting in a deadlock - Program exits
Second example
- The lock is acquired in the first goroutine but this takes some time to happen (??)
- Since there is no delay the
mainfunction acquires the lock before the goroutine can - Program exits