-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdining-philosophers.go
88 lines (78 loc) · 2.1 KB
/
dining-philosophers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
* Toy Go implementation of Dining Philosophers problem
* http://en.wikipedia.org/wiki/Dining_philosophers_problem
* Author: Doug Fritz
* Date: 2011-01-05
**/
package main
import (
"fmt"
"math/rand"
"time"
)
type Philosopher struct {
name string
chopstick chan bool
neighbor *Philosopher
}
func makePhilosopher(name string, neighbor *Philosopher) *Philosopher {
phil := &Philosopher{name, make(chan bool, 1), neighbor}
phil.chopstick <- true
return phil
}
func (phil *Philosopher) think() {
fmt.Printf("%v is thinking.\n", phil.name)
time.Sleep(time.Duration(rand.Int63n(1e9)))
}
func (phil *Philosopher) eat() {
fmt.Printf("%v is eating.\n", phil.name)
time.Sleep(time.Duration(rand.Int63n(1e9)))
}
func (phil *Philosopher) getChopsticks() {
timeout := make(chan bool, 1)
go func() { time.Sleep(1e9); timeout <- true }()
<-phil.chopstick
fmt.Printf("%v got his chopstick.\n", phil.name)
select {
case <-phil.neighbor.chopstick:
fmt.Printf("%v got %v's chopstick.\n", phil.name, phil.neighbor.name)
fmt.Printf("%v has two chopsticks.\n", phil.name)
return
case <-timeout:
phil.chopstick <- true
phil.think()
phil.getChopsticks()
}
}
func (phil *Philosopher) returnChopsticks() {
phil.chopstick <- true
phil.neighbor.chopstick <- true
}
func (phil *Philosopher) dine(announce chan *Philosopher) {
phil.think()
phil.getChopsticks()
phil.eat()
phil.returnChopsticks()
announce <- phil
}
func main() {
names := []string{"Kant", "Heidegger", "Wittgenstein",
"Locke", "Descartes", "Newton", "Hume", "Leibniz"}
philosophers := make([]*Philosopher, len(names))
var phil *Philosopher
for i, name := range names {
phil = makePhilosopher(name, phil)
philosophers[i] = phil
}
philosophers[0].neighbor = phil
fmt.Printf("There are %v philosophers sitting at a table.\n", len(philosophers))
fmt.Println("They each have one chopstick, and must borrow from their neighbor to eat.")
announce := make(chan *Philosopher)
for _, phil := range philosophers {
go phil.dine(announce)
}
for i := 0; i < len(names); i++ {
phil := <-announce
fmt.Printf("%v is done dining.\n", phil.name)
}
}