On Tue, Oct 10, 2017 at 7:56 AM, Jay Guo <guojiannan1...@gmail.com> wrote: > > In my use case, both cases you presented are valid and I don't really need > to disambiguate them. So I guess calling `Wait` before `Add` here should be > fine? I couldn't tell if there would be any unexpected crash from reading > `WaitGroup` source code.
It's fine to call Wait before Add, the Wait simply won't wait. Presumably you are then going to call Wait again on the same sync.WaitGroup, possibly after some Add calls have occurred. Ian > On Tue, Oct 10, 2017 at 9:42 PM, Ian Lance Taylor <i...@golang.org> wrote: >> >> On Tue, Oct 10, 2017 at 5:34 AM, Jay Guo <guojiannan1...@gmail.com> wrote: >> > >> > It is suggested in https://golang.org/pkg/sync/#WaitGroup that: >> > >> > // Note that calls with a positive delta that occur when the counter is >> > zero >> > // must happen before a Wait. >> > >> > I wonder what's the reason in terms of `sync` implementation. >> > >> > Essentially I'm trying to implement following logic: >> > >> > for { >> > waitForAllTaskToComplete() >> > ... >> > } >> > >> > where we may have no task in flight initially. >> >> That rule clarifies that if all the Done calls happen before the >> program gets around to calling Wait, the Wait will not block. >> Basically, a Wait with a count of zero might mean that nobody has >> called Add, or it might mean that all calls to Add have already called >> Done. Clearly in the latter case we should not block, and the effect >> is that we can not block in the former case as well. >> >> Ian > > -- 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.