package main
import (
"fmt"
"sync"
)
// 三个协程交替打印 cat dog fish
var repeatCount = 10
func main() {
// wg 用来防止主协程提前先退出
wg := &sync.WaitGroup{}
wg.Add(3)
chCat := make(chan struct{}, 1)
chDog := make(chan struct{}, 1)
chFish := make(chan struct{}, 1)
defer func() {
close(chCat)
close(chDog)
close(chFish)
}()
go printAnimal(chCat, chDog, "cat", wg)
go printAnimal(chDog, chFish, "dog", wg)
go printAnimal(chFish, chCat, "fish", wg)
chCat <- struct{}{}
wg.Wait()
}
// wg 需要传指针
func printAnimal(in, out chan struct{}, s string, wg *sync.WaitGroup) {
count := 0
for {
<-in
count++
fmt.Printf("%s,%d\n", s, count)
out <- struct{}{}
if count >= repeatCount {
wg.Done()
return
}
}
}
标签:wg,fish,chDog,dog,chFish,chCat,cat
From: https://www.cnblogs.com/qcy-blog/p/18020684