Move wg.Done in waitgroup worker to defer

Fixes #310
This commit is contained in:
Eli Bendersky
2020-01-07 06:05:34 -08:00
parent 6f7aa08f84
commit bd25cd7ee9
3 changed files with 32 additions and 21 deletions

View File

@@ -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() {

View File

@@ -1,2 +1,2 @@
39bbc00ecd87888761d480666e95d8b2c2a2a589
oBOGrV0n2Y2
fd77f5122e6df1669c0a2e0d2c4dfbd30631c21f
7mWXl0yVe6I