I have ran it, and it is alway the same order (three, one, two). Why is this, should it not be nondeterministic ?
package main
import (
"fmt"
"sync"
)
type Button struct {
Clicked *sync.Cond
}
func subscribe(c *sync.Cond, fn func()) {
var wg sync.WaitGroup
wg.Add(1)
go func() {
wg.Done()
c.L.Lock()
defer c.L.Unlock()
c.Wait()
fn()
}()
wg.Wait()
}
func main() {
button := Button{Clicked: sync.NewCond(&sync.Mutex{})}
var wg sync.WaitGroup
wg.Add(3)
subscribe(button.Clicked, func() {
fmt.Println("One")
wg.Done()
})
subscribe(button.Clicked, func() {
fmt.Println("Two")
wg.Done()
})
subscribe(button.Clicked, func() {
fmt.Println("Three")
wg.Done()
})
button.Clicked.Broadcast()
wg.Wait()
}
what do you think ?