3个函数分别打印dog、fish、cat,要求每个函数都要起一个goroutine,按照dog、fish、cat顺序打印在屏幕上10次。
通过channel传递信号,来控制goroutine的执行顺序
package main
import (
"fmt"
"sync"
"sync/atomic"
)
func main() {
var wg sync.WaitGroup
var dogcounter uint64
var fishcounter uint64
var catcounter uint64
wg.Add(3)
dogch := make(chan struct{}, 1)
fishch := make(chan struct{}, 1)
catch := make(chan struct{}, 1)
go dog(&wg, dogcounter, dogch, fishch)
go fish(&wg, fishcounter, fishch, catch)
go cat(&wg, catcounter, catch, dogch)
dogch <- struct{}{}
wg.Wait()
}
func dog(wg *sync.WaitGroup, counter uint64, dogch, fishch chan struct{}) {
for {
if counter >= uint64(10) {
wg.Done()
return
}
<-dogch
fmt.Println("dog")
atomic.AddUint64(&counter, 1)
fishch <- struct{}{}
}
}
func fish(wg *sync.WaitGroup, counter uint64, fishch, catch chan struct{}) {
for {
if counter >= uint64(10) {
wg.Done()
return
}
<-fishch
fmt.Println("fish")
atomic.AddUint64(&counter, 1)
catch <- struct{}{}
}
}
func cat(wg *sync.WaitGroup, counter uint64, catch, dogch chan struct{}) {
for {
if counter >= uint64(10) {
wg.Done()
return
}
<-catch
fmt.Println("cat")
atomic.AddUint64(&counter, 1)
dogch <- struct{}{}
}
}
package main
import (
"fmt"
"sync"
)
var dogcounter int
var fishcounter int
var catcounter int
func main() {
var wg sync.WaitGroup
wg.Add(3)
dogch := make(chan struct{}, 1)
fishch := make(chan struct{}, 1)
catch := make(chan struct{}, 1)
go dog(&wg, dogch, fishch)
go fish(&wg, fishch, catch)
go cat(&wg, catch, dogch)
dogch <- struct{}{}
wg.Wait()
}
func dog(wg *sync.WaitGroup, dogch, fishch chan struct{}) {
for {
if dogcounter >= 100 {
wg.Done()
return
}
<-dogch
fmt.Println("dog")
dogcounter++
fishch <- struct{}{}
}
}
func fish(wg *sync.WaitGroup, fishch, catch chan struct{}) {
for {
if fishcounter >= 100 {
wg.Done()
return
}
<-fishch
fmt.Println("fish")
fishcounter++
catch <- struct{}{}
}
}
func cat(wg *sync.WaitGroup, catch, dogch chan struct{}) {
for {
if catcounter >= 100 {
wg.Done()
return
}
<-catch
fmt.Println("cat")
catcounter++
dogch <- struct{}{}
}
}