当你调用Add(1)就会加1,Done() 或者Add(-1)就会减一,期间goroutine一直阻塞,直到WaitGroup计数值为0
示例
package main
import (
"fmt"
"sync"
)
var wp sync.WaitGroup
func showMsg(i int) {
// defer wp.Add(-1)
defer wp.Done()
fmt.Printf("i: %v\n", i)
}
func main() {
for i := 0; i < 10; i++ {
wp.Add(1)
go showMsg(i)
}
wp.Wait()
fmt.Println("end..")
}