summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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
}