Skip to content

Instantly share code, notes, and snippets.

@blanchonvincent
Created August 22, 2019 08:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blanchonvincent/5d003844134b81f1fdf525457fc97191 to your computer and use it in GitHub Desktop.
Save blanchonvincent/5d003844134b81f1fdf525457fc97191 to your computer and use it in GitHub Desktop.
Medium - Mutex starvation
func main() {
done := make(chan bool, 1)
var mu sync.Mutex
// goroutine 1
go func() {
for {
select {
case <-done:
return
default:
mu.Lock()
time.Sleep(100 * time.Microsecond)
mu.Unlock()
}
}
}()
// goroutine 2
for i := 0; i < 10; i++ {
time.Sleep(100 * time.Microsecond)
mu.Lock()
mu.Unlock()
}
done <- true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment