美文网首页
三个协程交替打印100次 cat dog fish

三个协程交替打印100次 cat dog fish

作者: overflow_e4e4 | 来源:发表于2023-02-01 16:55 被阅读0次
package main

import (
    "fmt"
    "sync"
)

// 三个协程交替打印 cat dog fish
var repeatCount = 100

func main() {
    // wg 用来防止主协程提前先退出
    wg := &sync.WaitGroup{}
    wg.Add(3)

    chCat := make(chan struct{}, 1)
    defer close(chCat)
    chDog := make(chan struct{}, 1)
    defer close(chDog)

    chFish := make(chan struct{}, 1)
    defer close(chFish)

    go printAnimal(&chCat, &chDog, "cat", wg)
    go printAnimal(&chDog, &chFish, "dog", wg)
    go printAnimal(&chFish, &chCat, "fish", wg)
    chCat <- struct{}{}
    wg.Wait()
}

// chan wg 需要传指针
func printAnimal(in, out *chan struct{}, s string, wg *sync.WaitGroup) {
    count := 0
    for {
        <-*in
        count++
        fmt.Println(s)
        *out <- struct{}{}

        if count >= repeatCount {
            wg.Done()
            return
        }

    }
}


相关文章

网友评论

      本文标题:三个协程交替打印100次 cat dog fish

      本文链接:https://www.haomeiwen.com/subject/dnvihdtx.html