Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
9 lines (6 sloc) 667 Bytes

当Add()和Done()的数量不匹配时会发生什么?

当sync.Add()和sync.Done()调用的数量相等时,程序会正常运行。但是,本节将告诉您当调用数量不一致时会发生什么。

假如我们执行sync.Add()的次数大于执行sync.Done()的次数,这种情况下,通过在第一个fmt.Printf(“%#v \ n”,waitGroup)之前添加waitGroup.Add(1)语句,然后执行go run的输出如下:

$ go run syncGo.go Going to create 20 goroutines. sync.WaitGroup{noCopy:sync.noCopy{}, state1:[12]uint8{0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, sema:0x0} sync.WaitGroup{noCopy:sync.noCopy{}, ...
You can’t perform that action at this time.