From bd25cd7ee9ee9cbe1b7e9375dfcf2b91cf98b20c Mon Sep 17 00:00:00 2001
From: Eli Bendersky
Date: Tue, 7 Jan 2020 06:05:34 -0800
Subject: [PATCH] Move wg.Done in waitgroup worker to defer
Fixes #310
---
examples/waitgroups/waitgroups.go | 6 ++--
examples/waitgroups/waitgroups.hash | 4 +--
public/waitgroups | 43 ++++++++++++++++++-----------
3 files changed, 32 insertions(+), 21 deletions(-)
diff --git a/examples/waitgroups/waitgroups.go b/examples/waitgroups/waitgroups.go
index 2cf16a3..c3c8580 100644
--- a/examples/waitgroups/waitgroups.go
+++ b/examples/waitgroups/waitgroups.go
@@ -13,14 +13,14 @@ import (
// Note that a WaitGroup must be passed to functions by
// pointer.
func worker(id int, wg *sync.WaitGroup) {
+ // On return, notify the WaitGroup that we're done.
+ defer wg.Done()
+
fmt.Printf("Worker %d starting\n", id)
// Sleep to simulate an expensive task.
time.Sleep(time.Second)
fmt.Printf("Worker %d done\n", id)
-
- // Notify the WaitGroup that this worker is done.
- wg.Done()
}
func main() {
diff --git a/examples/waitgroups/waitgroups.hash b/examples/waitgroups/waitgroups.hash
index f944ca9..ab9311b 100644
--- a/examples/waitgroups/waitgroups.hash
+++ b/examples/waitgroups/waitgroups.hash
@@ -1,2 +1,2 @@
-39bbc00ecd87888761d480666e95d8b2c2a2a589
-oBOGrV0n2Y2
+fd77f5122e6df1669c0a2e0d2c4dfbd30631c21f
+7mWXl0yVe6I
diff --git a/public/waitgroups b/public/waitgroups
index e9d39c7..e47765d 100644
--- a/public/waitgroups
+++ b/public/waitgroups
@@ -42,7 +42,7 @@ use a wait group.
- 
+ 
@@ -75,7 +75,31 @@ pointer.
|
func worker(id int, wg *sync.WaitGroup) {
- fmt.Printf("Worker %d starting\n", id)
+
+
+ |
+
+
+
+
+ On return, notify the WaitGroup that we’re done.
+
+ |
+
+
+
+
+ |
+
+
+
+
+
+ |
+
+
+ fmt.Printf("Worker %d starting\n", id)
|
@@ -90,19 +114,6 @@ pointer.
time.Sleep(time.Second)
fmt.Printf("Worker %d done\n", id)
-
-
-
-
-
-
-
- Notify the WaitGroup that this worker is done.
-
- |
-
-
-
@@ -218,7 +229,7 @@ is likely to be different for each invocation.
|