summaryrefslogtreecommitdiff
path: root/main.go
blob: 23275d12d0ec50616b36442fdb3895931982c9e5 (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
37
38
39
40
41
package main

import (
	"flag"
	"math/rand"
	"sync"
	"time"
)

func init() {
	rand.Seed(time.Now().UnixNano())
}

func main() {
	names := Names{"Aristotle", "Kant", "Spinoza", "Marx", "Russell"}
	flag.Var(&names, "names", "philospher names")
	bites := flag.Int("bites", 3, "number of rounds")
	delay := 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)],
			Bites:    *bites,
			MaxDelay: *delay,
			TimeOut:  *timeOut,
		}
		go func() {
			p.Dine()
			wg.Done()
		}()
	}
	wg.Wait()
}