From 936e13ba9225e271911bf097e9f918c80f1d361d Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sun, 2 Apr 2017 15:41:52 +0200 Subject: Add report channel --- main.go | 11 ++++++++++- philo.go | 3 ++- 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 { -- cgit v1.2.3