summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.go11
-rw-r--r--philo.go3
2 files changed, 12 insertions, 2 deletions
diff --git a/main.go b/main.go
index c955fde..08646a7 100644
--- a/main.go
+++ b/main.go
@@ -2,6 +2,7 @@ package main
import (
"flag"
+ "fmt"
"sync"
"time"
)
@@ -15,6 +16,7 @@ func main() {
flag.Parse()
forks := NewForks(len(names))
+ reports := make(chan string)
wg := sync.WaitGroup{}
for i, name := range names {
@@ -26,11 +28,18 @@ func main() {
Rounds: *rounds,
MaxDelay: *maxDelay,
TimeOut: *timeOut,
+ Report: reports,
}
go func() {
p.Dine()
wg.Done()
}()
}
- wg.Wait()
+ go func() {
+ wg.Wait()
+ close(reports)
+ }()
+ for v := range reports {
+ fmt.Println(v)
+ }
}
diff --git a/philo.go b/philo.go
index 7007a7a..f14c095 100644
--- a/philo.go
+++ b/philo.go
@@ -17,6 +17,7 @@ type Philo struct {
Rounds int
MaxDelay time.Duration
TimeOut time.Duration
+ Report chan string
}
func (p Philo) Delay() {
@@ -25,7 +26,7 @@ func (p Philo) Delay() {
}
func (p Philo) Print(s string) {
- fmt.Printf("%10s %s\n", p.Name, s)
+ p.Report <- fmt.Sprintf("%10s %s", p.Name, s)
}
func (p Philo) Arrive() stateFn {