From d5a663995dcce996bdfeb08f411cb69217e3e32e Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Tue, 22 Sep 2015 20:49:43 +0200 Subject: Add new state --- philo.go | 15 +++++++++++---- 1 file 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 -- cgit v1.2.3