> Well, I figure that the race condition is benign here, since even if a
> goroutine sees an old value, all I care about is that by the time wg.Wait()
> is finished, condition is either set or unset. But please correct me if I'm
> wrong about that.
In general all races should be avoided. I agree that this one is
likely benign.
> I like the idea of using channels but I couldn't figure out how to get it
> playing nicely with WaitGroup. In your example, you do:
>
> wg.Wait()
> close(c)
> condition, _ := <-c
>
> But I am under the impression that reading from a closed channel always
> returns a zero value, so condition would always be false in this case. Is
> that not true here?
A close of a channel is in effect sent on the channel. The close will
only be seen after all other values have been read. So in this case
if any goroutine wrote true to the channel, that is the value that
will wind up in the variable "condition". If no goroutine wrote to
the channel, then it will be closed, and "condition" will be set to
the zero value == false.
In fact I now see that the _ is pointless and it would fine to write
simply
condition := <-c
> Otherwise, if we don't close the channel and just try to
> do "condition := <-c", then it would hang if "c <- true" is never called by
> any goroutine. Although, perhaps I could use a select statement after
> wg,Wait(), as in:
>
> wg.Wait()
> select {
> case <-c:
> // do stuff with condition == true
> default:
> // do other stuff
> }
>
> Does that seem reasonable?
Yes, that will also work, it's just more verbose.
Ian