Why not defer wg.Done() in WaitGroup?

1,179 views
Skip to first unread message

Thomas Kappler

unread,
Dec 10, 2012, 11:22:53 AM12/10/12
to golan...@googlegroups.com
This question is about the WaitGroup example in the sync package docs: http://golang.org/pkg/sync/#example_WaitGroup

The subject says it already: when a goroutine is tracked by a WaitGroup, is there any reason not to start it with "defer wg.Done()"? The same reasoning than for defering the closing of files etc. applies, one cannot forget to call Done() when, e.g., returning early after an error.

Patrick Mylund Nielsen

unread,
Dec 10, 2012, 11:26:20 AM12/10/12
to Thomas Kappler, golang-nuts
Not if what you're waiting for are the goroutines


On Mon, Dec 10, 2012 at 5:22 PM, Thomas Kappler <tkap...@gmail.com> wrote:
This question is about the WaitGroup example in the sync package docs: http://golang.org/pkg/sync/#example_WaitGroup

The subject says it already: when a goroutine is tracked by a WaitGroup, is there any reason not to start it with "defer wg.Done()"? The same reasoning than for defering the closing of files etc. applies, one cannot forget to call Done() when, e.g., returning early after an error.

--
 
 

roger peppe

unread,
Dec 10, 2012, 12:18:01 PM12/10/12
to Thomas Kappler, golang-nuts
> when a goroutine is tracked by a WaitGroup, is there any reason not to start it with "defer wg.Done()"?

no. that's how i usually use it.
> --
>
>
Reply all
Reply to author
Forward
0 new messages