调用后可以sync.WaitGroup
重复使用吗?Wait()
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按预期运行,但能保证安全吗?文档没有这么说,但也许我只是偏执。