Mistake #72's solution might not work
Author: mtfcdCreated Dec 20, 2024Updated Dec 20, 2024
Labelserratum
Describe the book error
The code in the book works because the updater goroutine sleeps for 1 second before acquiring the lock. This ensures that the Listener goroutines have time to acquire the lock and check if the condition is met. However, if the sleep is removed or reduced to a very short duration (e.g., time.Nanosecond), the code may not work as expected.
I believe there might be two reasons for this:
- The Updater goroutine competes with the Listener goroutines, preventing them from acquiring the lock.
- The two Listener goroutines compete with each other. For example, the
f(15)goroutine might consistently succeed in acquiring the lock. Afterf(15)releases the lock, the Updater goroutine might acquire it, which could prevent thef(10)goroutine from ever getting the lock.
Source: teivah/100-go-mistakes