From a9a2e42b4162ebfb9fe908188b6471aedfa9f669 Mon Sep 17 00:00:00 2001 From: vinicius Date: Tue, 19 Mar 2024 15:48:04 -0300 Subject: [PATCH] Write summary for mistake 71 --- docs/index.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/docs/index.md b/docs/index.md index ca9333b..d1a2a28 100644 --- a/docs/index.md +++ b/docs/index.md @@ -1926,6 +1926,24 @@ In summary, we have to be careful with the boundaries of a mutex lock. In this s To accurately use `sync.WaitGroup`, call the `Add` method before spinning up goroutines. +The `Add` method of `sync.WaitGroup` is used to increment an internal counter of goroutines that the program should wait for. Therefore, it is important to call `Add` before starting the goroutine. If you start the goroutine before calling `Add`, there is a possibility that the goroutine will finish before `Add` is called, which may lead to an incorrect wait state. + +```go +var wg sync.WaitGroup + +for i := 0; i < 5; i++ { + wg.Add(1) // Adds to counter before starting goroutine + go func(i int) { + defer wg.Done() // Decrease the counter when the goroutine ends + fmt.Println(i) + }(i) +} + +wg.Wait() // Wait until all goroutines finish +``` + +In this example, `wg.Add(1)` is called before each goroutine is started, ensuring the counter is correct. When each goroutine finishes, it calls `wg.Done()` to decrement the counter. Finally, `wg.Wait()` is used to block until all goroutines have finished. This ensures that the main program does not terminate before all goroutines have completed their work. + [:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/09-concurrency-practice/71-wait-group/main.go) ### Forgetting about `sync.Cond` (#72)