Last active
March 4, 2016 20:23
-
-
Save larryprice/4b129f43424ae74e1941 to your computer and use it in GitHub Desktop.
Dining philosophers with channels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"time" | |
) | |
type Philosopher struct { | |
Name string | |
} | |
func (p *Philosopher) Eat(left, right chan bool) { | |
for { | |
Left: | |
select { | |
case <-left: | |
for { | |
select { | |
case <-right: | |
fmt.Println(p.Name, "is eating!") | |
time.Sleep(time.Second) | |
left <- true | |
right <- true | |
break Left | |
default: | |
left <- true | |
break Left | |
} | |
} | |
default: | |
time.Sleep(100 * time.Millisecond) | |
} | |
} | |
} | |
func eat(p Philosopher, left, right chan bool) { | |
go p.Eat(left, right) | |
} | |
func main() { | |
p := []Philosopher{ | |
Philosopher{"A"}, | |
Philosopher{"B"}, | |
Philosopher{"C"}, | |
Philosopher{"D"}, | |
Philosopher{"E"}, | |
} | |
forks := []chan bool{} | |
for range p { | |
forks = append(forks, make(chan bool, 1)) | |
} | |
for i, pp := range p { | |
eat(pp, forks[i], forks[(i+1)%len(forks)]) | |
} | |
for i := range forks { | |
forks[i] <- true | |
} | |
time.Sleep(10 * time.Second) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment