From 57cb1d464d3791a51eb78169d0f470097162daea Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Wed, 24 Aug 2016 23:10:42 +0200 Subject: Actually use forks --- philo.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/philo.go b/philo.go index 0256aeb..866fcd3 100644 --- a/philo.go +++ b/philo.go @@ -20,10 +20,12 @@ var philo = []string{ "Russell", } +type Fork struct{} + type Philo struct { Name string - LHS chan bool - RHS chan bool + LHS chan Fork + RHS chan Fork Bites int } @@ -53,7 +55,7 @@ func hungry(p *Philo) stateFn { case <-p.RHS: // try to grab right fork return eat case <-time.After(time.Second): - p.LHS <- true // put left fork back + p.LHS <- Fork{} // put left fork back return starve } @@ -71,8 +73,8 @@ func eat(p *Philo) stateFn { p.state("is eating") rndDelay() - p.LHS <- true // release left fork - p.RHS <- true // release right fork + p.LHS <- Fork{} // release left fork + p.RHS <- Fork{} // release right fork if p.Bites -= 1; p.Bites <= 0 { return leave @@ -94,12 +96,12 @@ func leave(p *Philo) stateFn { return nil } -func prepare(n int) []chan bool { - forks := make([]chan bool, n) +func prepare(n int) []chan Fork { + forks := make([]chan Fork, n) for i := range forks { - forks[i] = make(chan bool, 1) - forks[i] <- true // put a fork + forks[i] = make(chan Fork, 1) + forks[i] <- Fork{} // put a fork } return forks -- cgit v1.2.3