summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--philo.go15
1 files changed, 11 insertions, 4 deletions
diff --git a/philo.go b/philo.go
index 4eb1b84..19e555a 100644
--- a/philo.go
+++ b/philo.go
@@ -48,24 +48,31 @@ func hungry(p *Philo) stateFn {
p.state("is hungry")
<-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 starving
}
return eat
}
+func starving(p *Philo) stateFn {
+ p.state("is starving")
+ rndDelay()
+
+ return hungry
+}
+
func eat(p *Philo) stateFn {
p.state("is eating")
rndDelay()
- p.LHS <- true // release a fork
- p.RHS <- true // release a fork
+ p.LHS <- true // release left fork
+ p.RHS <- true // release right fork
if p.Bites -= 1; p.Bites <= 0 {
return leave