Advertisement
Guest User

Untitled

a guest
May 22nd, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.86 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "sync"
  6. )
  7.  
  8. type Button struct {
  9.     Clicked *sync.Cond
  10. }
  11.  
  12. func main() {
  13.     button := Button{ Clicked: sync.NewCond(&sync.Mutex{})}
  14.  
  15.     subscribe := func(c *sync.Cond, fn func()) {
  16.         var goroutineRunning sync.WaitGroup
  17.         goroutineRunning.Add(1)
  18.  
  19.         go func() {
  20.             defer func() {
  21.  
  22.                 c.L.Unlock()
  23.             }()
  24.             goroutineRunning.Done()
  25.  
  26.             c.L.Lock()
  27.             c.Wait()
  28.  
  29.             fn()
  30.         }()
  31.  
  32.         goroutineRunning.Wait()
  33.  
  34.     }
  35.  
  36.     var clickRegistered sync.WaitGroup
  37.     clickRegistered.Add(3)
  38.     subscribe(button.Clicked, func() {
  39.         fmt.Println("Maximizing window.")
  40.         clickRegistered.Done()
  41.     })
  42.     subscribe(button.Clicked, func() {
  43.         fmt.Println("Dialog box.")
  44.         clickRegistered.Done()
  45.     })
  46.     subscribe(button.Clicked, func() {
  47.         fmt.Println("Click.")
  48.         clickRegistered.Done()
  49.     })
  50.  
  51.     button.Clicked.Broadcast()
  52.     clickRegistered.Wait()
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement