From 75d369a50e15ec3faa94ef3227afd2ccf2c1404b Mon Sep 17 00:00:00 2001 From: Oscar Landry L Date: Tue, 7 May 2019 15:45:05 +0200 Subject: [PATCH 1/2] fix anonym function use anonym function param --- exercises/basics/06-concurrency/01-goroutines/src_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/basics/06-concurrency/01-goroutines/src_test.go b/exercises/basics/06-concurrency/01-goroutines/src_test.go index d43bc16..35c531f 100644 --- a/exercises/basics/06-concurrency/01-goroutines/src_test.go +++ b/exercises/basics/06-concurrency/01-goroutines/src_test.go @@ -14,7 +14,7 @@ func TestExecute(t *testing.T) { func() { buffers := make(chan *bytes.Buffer) go func(done chan *bytes.Buffer) { - buffers <- Execute(10) + done <- Execute(10) }(buffers) select { From 8df9ffa75fa28c21d324b5c73f1af289a09c7b40 Mon Sep 17 00:00:00 2001 From: Oscar Landry L Date: Tue, 7 May 2019 15:47:31 +0200 Subject: [PATCH 2/2] fix race conditions add mutex to avoid access concurrent on buffer writing process --- solutions/basics/06-concurrency/01-goroutines/src.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/solutions/basics/06-concurrency/01-goroutines/src.go b/solutions/basics/06-concurrency/01-goroutines/src.go index 0c6a233..1a540d6 100644 --- a/solutions/basics/06-concurrency/01-goroutines/src.go +++ b/solutions/basics/06-concurrency/01-goroutines/src.go @@ -10,6 +10,9 @@ import ( // Maybe if we ran all the long processes concurrently, it would be much faster. // Let's do that! // Beware, all the processes must end before that Execute returns. + +var m sync.Mutex + func Execute(nbProcess int) *bytes.Buffer { buf := new(bytes.Buffer) wg := &sync.WaitGroup{} @@ -26,5 +29,7 @@ func Execute(nbProcess int) *bytes.Buffer { func someReallyLongProcess(buf *bytes.Buffer) { time.Sleep(1 * time.Second) + m.Lock() + defer m.Unlock() buf.WriteString("Done\n") }