Go: Mutex and Starvation
ℹ️ This article is based on Go 1.13.
While developing in Golang, a mutex can encounter starvation issues when it consistently tries to acquire a lock that it is never able to get. For this article, we’ll look at a starvation issue affecting Go 1.8 that was solved in Go 1.9.
Starvation
In order to illustrate a starvation situation with a mutex, I will take the example made by Russ Cox about the issue in which they discuss mutex improvement:
This example is based on two goroutines:
- goroutine 1 holds the lock for a long time and briefly releases it
- goroutine 2 briefly holds the lock and releases it for a long time
Both have a cycle of 100 microseconds, but since goroutine 1 is constantly requesting the lock, we could expect it will get the lock more often.
Here is an example, done with Go 1.8, of the lock distribution with a loop of 10 iterations:
Lock acquired per goroutine:
g1: 7200216
g2: 10