2
Wait()
被調用後可否重複使用sync.WaitGroup
?sync.WaitGroup可以安全地重用嗎?
func worker(who string, in <-chan int, wg *sync.WaitGroup) {
for i := range in {
fmt.Println(who, i)
wg.Done()
}
}
func main() {
var wg sync.WaitGroup
AIn := make(chan int, 1)
BIn := make(chan int, 1)
go worker("a:", AIn, &wg)
go worker("b:", BIn, &wg)
for i := 0; i < 4; i++ {
wg.Add(2)
AIn <- i
BIn <- i
wg.Wait()
fmt.Println("main:", i)
}
}
這play.golang.org/p/QLsvA-b4Ae按預期運行,但它是保證是安全的嗎?文檔沒有這麼說,但也許我只是偏執狂。