summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2015-09-22 20:33:57 +0200
committerDimitri Sokolyuk <demon@dim13.org>2015-09-22 20:33:57 +0200
commit37c821914f39df6097d88d68ac0750a0667a507a (patch)
tree28357c9847613a81ac85d95dca75dd0cd6645b79
parent8c6ea047f23e39deb06979fe65d16883339db3e7 (diff)
Avoid deadlock
-rw-r--r--philo.go11
1 files changed, 9 insertions, 2 deletions
diff --git a/philo.go b/philo.go
index 43bc6c7..4eb1b84 100644
--- a/philo.go
+++ b/philo.go
@@ -47,8 +47,15 @@ func arrive(p *Philo) stateFn {
func hungry(p *Philo) stateFn {
p.state("is hungry")
- <-p.LHS // grab a fork
- <-p.RHS // grab a fork
+ <-p.LHS // grab left fork
+ select {
+ case <-p.RHS: // try to grab right fork
+ return eat
+ case <-time.After(time.Second):
+ p.LHS <- true // put left fork back
+ rndDelay()
+ return hungry
+ }
return eat
}