summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-08-24 23:10:42 +0200
committerDimitri Sokolyuk <demon@dim13.org>2016-08-24 23:10:42 +0200
commit57cb1d464d3791a51eb78169d0f470097162daea (patch)
treee3644a2a211d8e6f265bf6f2260d61b2de703e1c
parent8d3826dc3143bffb048fe6867f3f9040914680c3 (diff)
Actually use forks
-rw-r--r--philo.go20
1 files 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