summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2015-09-22 20:49:43 +0200
committerDimitri Sokolyuk <demon@dim13.org>2015-09-22 20:49:43 +0200
commitd5a663995dcce996bdfeb08f411cb69217e3e32e (patch)
treeb6e0e9a5906ba8f7206e7a0532a93137786fc2c9
parent37c821914f39df6097d88d68ac0750a0667a507a (diff)
Add new state
-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