package main
import (
"fmt"
"sync"
)
func main() {
type Button struct {
Clicked *sync.Cond
}
button := Button{Clicked: sync.NewCond(&sync.Mutex{})}
subscribe := func(c *sync.Cond, fn func()) {
var goroutineRunning sync.WaitGroup
goroutineRunning.Add(1)
go func() {
goroutineRunning.Done()
c.L.Lock()
defer c.L.Unlock()
c.Wait()
fn()
}()
goroutineRunning.Wait()
}
var clickRegistered sync.WaitGroup
clickRegistered.Add(3)
subscribe(button.Clicked, func() {
fmt.Println("Maximium Window")
clickRegistered.Done()
})
subscribe(button.Clicked, func() {
fmt.Println("Displaying Annoying dialog box")
clickRegistered.Done()
})
subscribe(button.Clicked, func() {
fmt.Println("Mouse Clicked")
clickRegistered.Done()
})
button.Clicked.Broadcast()
clickRegistered.Wait()
}
程序输出如下,
有疑问加站长微信联系(非本文作者)