but if the event happens only once, then closing a channel works well too,
because an indeterminate number of reading goroutines can
all be woken with that single action.
one advantage of using a channel is that it is easily added to
a select with other channels where using sync.Cond would require an
extra waiting goroutine.
On 20 September 2011 08:01, Robert Bloomquist
but if the event happens only once, then closing a channel works well too,
I highly recommend using channels for this. Lock-based "broadcast" messages are very difficult to reason about, sync.Cond doubly so---for instance, the following does not work as one might expect:l := new(sync.Mutex)c := sync.NewCond(l)for i := 0; i < 10; i++ {go func() {l.Lock()defer l.Unlock()c.Wait()fmt.Println(i)}()}c.Broadcast()
Am I correct in believing that this may fail because the call to c.Broadcast() may happen before all goroutines have executed c.Wait()I highly recommend using channels for this. Lock-based "broadcast" messages are very difficult to reason about, sync.Cond doubly so---for instance, the following does not work as one might expect:l := new(sync.Mutex)c := sync.NewCond(l)for i := 0; i < 10; i++ {go func() {l.Lock()defer l.Unlock()c.Wait()fmt.Println(i)}()}c.Broadcast()