summaryrefslogtreecommitdiff
path: root/main.go
blob: c955fde19e437cedadac14238a84b345dec36655 (plain)
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
package main

import (
	"flag"
	"sync"
	"time"
)

func main() {
	names := Names{"Aristotle", "Kant", "Spinoza", "Marx", "Russell"}
	flag.Var(&names, "names", "philospher names")
	rounds := flag.Int("rounds", 3, "number of rounds")
	maxDelay := flag.Duration("max delay", 3*time.Second, "delay")
	timeOut := flag.Duration("timeout", time.Second, "delay")
	flag.Parse()

	forks := NewForks(len(names))

	wg := sync.WaitGroup{}
	for i, name := range names {
		wg.Add(1)
		p := &Philo{
			Name:     name,
			Left:     forks[i],
			Right:    forks[(i+1)%len(names)],
			Rounds:   *rounds,
			MaxDelay: *maxDelay,
			TimeOut:  *timeOut,
		}
		go func() {
			p.Dine()
			wg.Done()
		}()
	}
	wg.Wait()
}