-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
58 lines (54 loc) · 1.55 KB
/
main.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
package main
import (
"philosophers/phils"
"sync"
)
func main() {
lunchTable := phils.NewLunchTable(6)
// создаём срез индексов всех философов, чтобы потом не искать их в основном
philsIndexes := make([]int, 6)
var err error
if philsIndexes[0], err = lunchTable.AddPhilosopher(phils.Philosopher{"Аристотель"}); err != nil {
panic(err)
}
if philsIndexes[1], err = lunchTable.AddPhilosopher(phils.Philosopher{"Платон"}); err != nil {
panic(err)
}
if philsIndexes[2], err = lunchTable.AddPhilosopher(phils.Philosopher{"Сократ"}); err != nil {
panic(err)
}
if philsIndexes[3], err = lunchTable.AddPhilosopher(phils.Philosopher{"Демокрит"}); err != nil {
panic(err)
}
if philsIndexes[4], err = lunchTable.AddPhilosopher(phils.Philosopher{"Маркс"}); err != nil {
panic(err)
}
if philsIndexes[5], err = lunchTable.AddPhilosopher(phils.Philosopher{"Кант"}); err != nil {
panic(err)
}
for _, index := range philsIndexes {
go run(index, &lunchTable)
}
for {
}
}
func run(index int, lunchTable *phils.LunchTable) {
var leftFork *sync.Mutex
var rightFork *sync.Mutex
switch val := lunchTable.Table[index-1].(type) {
case *sync.Mutex:
leftFork = val
}
if index+1 < len(lunchTable.Table) {
switch val := lunchTable.Table[index+1].(type) {
case *sync.Mutex:
rightFork = val
}
} else {
switch val := lunchTable.Table[0].(type) {
case *sync.Mutex:
rightFork = val
}
}
lunchTable.Table[index].(phils.Philosopher).Live(leftFork, rightFork)
}