-
Notifications
You must be signed in to change notification settings - Fork 7
/
wait-group.go
26 lines (21 loc) · 918 Bytes
/
wait-group.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package main
import "fmt"
import "sync"
var wg sync.WaitGroup // 1
func routine(i int) {
defer wg.Done() // 3
fmt.Printf("routine %v finished\n", i)
}
func main() {
for i := 0; i < 10; i++ {
wg.Add(1) // 2
go routine(i) // *
}
wg.Wait() // 4
fmt.Println("main finished")
}
// WaitGroup usage in order of execution.
// Declaration of global variable. Making it global is the easiest way to make it visible to all functions and methods.
// Increasing the counter. This must be done in main goroutine because there is no guarantee that newly started goroutine will execute before 4 due to memory model guarantees.
// Decreasing the counter. This must be done at the exit of goroutine. Using deferred call, we make sure that it will be called whenever function ends no matter but no matter how it ends.
// Waiting for the counter to reach 0. This must be done in main goroutine to prevent program exit.