-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphilosopher.go
76 lines (66 loc) · 1.59 KB
/
philosopher.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
package main
import (
"fmt"
"math/rand"
"time"
)
const (
eat = 150 // Time spent eating in milliseconds
think = 100 // Time spent thinking in milliseconds
)
// A philosopher has an ID a chopstick for each hand,
// access to the waiter and a plate of food
type Philosopher struct {
id int
left, right *ChopStick
waiter *Waiter
done chan bool
food Food
}
// This philosopher will have a session of thinking and eating until
// all food portions are gone.
func (p *Philosopher) start() {
for ; p.food.portions > 0; p.food.portions-- {
p.timeout(think)
p.eat()
}
// Send message into channel to acknowledge this philosopher has finished
p.done <- true
}
// Before a philosopher can eat it needs permission from the waiter.
// Once granted permission the philosopher can pick up two chopsticks
// and begin to eat.
func (p *Philosopher) eat() {
p.waiter.askPermissionToEat(p)
fmt.Println("Phil", p.id, "is eating...")
// Take some time eating.
p.timeout(eat)
fmt.Println("Phil", p.id, "finished")
p.waiter.onFinishEating(p)
}
// Sets a timeout for a specific duration
func (p *Philosopher) timeout(duration time.Duration) {
time.Sleep(duration * time.Millisecond)
}
// Put down chopsticks in a random order
func (p *Philosopher) putDownChopSticks() {
switch rand.Intn(2) {
case 0:
p.left.Unlock()
p.right.Unlock()
case 1:
p.right.Unlock()
p.left.Unlock()
}
}
// Pick up chopsticks in a random order
func (p *Philosopher) pickUpChopSticks() {
switch rand.Intn(2) {
case 0:
p.left.Lock()
p.right.Lock()
case 1:
p.right.Lock()
p.left.Lock()
}
}