It is safe to call wg.Add fron multiple goroutines. The only caveat is that 
wg.Wait must be synchronized with the first wg.Add (which it is in your 
case because you call wg.Add(1) in the same goroutine as wg.Wait()).

The go source code is very readable, and easily accessible from godoc. 
 Here's the implementation of Add()

https://golang.org/src/sync/waitgroup.go?s=1857:1892#L42

The underlying implementation of wg.Add uses atomic.UInt64Add to increment 
values and semaphores to do block and release waiting goroutines


On Sunday, January 1, 2017 at 3:53:45 PM UTC-5, San wrote:
>
> I don't see any mention about the safety of using WaitGroup from multiple 
> goroutines in godoc.
> I stumble upon "new Add calls must happen after all previous Wait calls 
> have returned."
> but given the use case of WaitGroup and the function name I am not quite 
> sure about it.
>
> For example in the last exercise of go tour.
> I use WaitGroup like this
> https://play.golang.org/p/096dxnsY98
> If I naively interpret the doc I'll guess that I should not call Add 
> during Wait at all.
> I wonder if this actually safe or not?
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to