summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'main.go')
-rw-r--r--main.go41
1 files changed, 41 insertions, 0 deletions
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..23275d1
--- /dev/null
+++ b/main.go
@@ -0,0 +1,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()
+}