Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "sync"
- )
- type Button struct {
- Clicked *sync.Cond
- }
- func main() {
- button := Button{ Clicked: sync.NewCond(&sync.Mutex{})}
- subscribe := func(c *sync.Cond, fn func()) {
- var goroutineRunning sync.WaitGroup
- goroutineRunning.Add(1)
- go func() {
- defer func() {
- c.L.Unlock()
- }()
- goroutineRunning.Done()
- c.L.Lock()
- c.Wait()
- fn()
- }()
- goroutineRunning.Wait()
- }
- var clickRegistered sync.WaitGroup
- clickRegistered.Add(3)
- subscribe(button.Clicked, func() {
- fmt.Println("Maximizing window.")
- clickRegistered.Done()
- })
- subscribe(button.Clicked, func() {
- fmt.Println("Dialog box.")
- clickRegistered.Done()
- })
- subscribe(button.Clicked, func() {
- fmt.Println("Click.")
- clickRegistered.Done()
- })
- button.Clicked.Broadcast()
- clickRegistered.Wait()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement