Skip to content

Commit 7a2689b

Browse files
aclementsgopherbot
authored andcommitted
sync: tidy WaitGroup documentation, add WaitGroup.Go example
This reframes the WaitGroup documentation with Go at its center and Add/Done as more "advanced" features. Updates #63796 Change-Id: I8101972626fdb00c6f7fb185b685227823d10db1 Reviewed-on: https://go-review.googlesource.com/c/go/+/662975 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Alan Donovan <[email protected]> Auto-Submit: Austin Clements <[email protected]> Reviewed-by: Carlos Amedee <[email protected]>
1 parent 9f55e7b commit 7a2689b

File tree

2 files changed

+61
-30
lines changed

2 files changed

+61
-30
lines changed

src/sync/example_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,26 @@ var http httpPkg
1919
// This example fetches several URLs concurrently,
2020
// using a WaitGroup to block until all the fetches are complete.
2121
func ExampleWaitGroup() {
22+
var wg sync.WaitGroup
23+
var urls = []string{
24+
"http://www.golang.org/",
25+
"http://www.google.com/",
26+
"http://www.example.com/",
27+
}
28+
for _, url := range urls {
29+
// Launch a goroutine to fetch the URL.
30+
wg.Go(func() {
31+
// Fetch the URL.
32+
http.Get(url)
33+
})
34+
}
35+
// Wait for all HTTP fetches to complete.
36+
wg.Wait()
37+
}
38+
39+
// This example is equivalent to the main example, but uses Add/Done
40+
// instead of Go.
41+
func ExampleWaitGroup_addAndDone() {
2242
var wg sync.WaitGroup
2343
var urls = []string{
2444
"http://www.golang.org/",

src/sync/waitgroup.go

+41-30
Original file line numberDiff line numberDiff line change
@@ -11,51 +11,52 @@ import (
1111
)
1212

1313
// A WaitGroup is a counting semaphore typically used to wait
14-
// for a group of goroutines to finish.
14+
// for a group of goroutines or tasks to finish.
1515
//
16-
// The main goroutine calls [WaitGroup.Add] to set (or increase) the number of
17-
// goroutines to wait for. Then each of the goroutines
18-
// runs and calls [WaitGroup.Done] when finished. At the same time,
19-
// [WaitGroup.Wait] can be used to block until all goroutines have finished.
20-
//
21-
// This is a typical pattern of WaitGroup usage to
22-
// synchronize 3 goroutines, each calling the function f:
16+
// Typically, a main goroutine will start tasks, each in a new
17+
// goroutine, by calling [WaitGroup.Go] and then wait for all tasks to
18+
// complete by calling [WaitGroup.Wait]. For example:
2319
//
2420
// var wg sync.WaitGroup
25-
// for range 3 {
26-
// wg.Add(1)
27-
// go func() {
28-
// defer wg.Done()
29-
// f()
30-
// }()
31-
// }
21+
// wg.Go(task1)
22+
// wg.Go(task2)
3223
// wg.Wait()
3324
//
34-
// For convenience, the [WaitGroup.Go] method simplifies this pattern to:
25+
// A WaitGroup may also be used for tracking tasks without using Go to
26+
// start new goroutines by using [WaitGroup.Add] and [WaitGroup.Done].
27+
//
28+
// The previous example can be rewritten using explicitly created
29+
// goroutines along with Add and Done:
3530
//
3631
// var wg sync.WaitGroup
37-
// for range 3 {
38-
// wg.Go(f)
39-
// }
32+
// wg.Add(1)
33+
// go func() {
34+
// defer wg.Done()
35+
// task1()
36+
// }()
37+
// wg.Add(1)
38+
// go func() {
39+
// defer wg.Done()
40+
// task2()
41+
// }()
4042
// wg.Wait()
4143
//
42-
// A WaitGroup must not be copied after first use.
43-
//
44-
// In the terminology of [the Go memory model], a call to [WaitGroup.Done]
45-
// “synchronizes before” the return of any Wait call that it unblocks.
44+
// This pattern is common in code that predates [WaitGroup.Go].
4645
//
47-
// [the Go memory model]: https://go.dev/ref/mem
46+
// A WaitGroup must not be copied after first use.
4847
type WaitGroup struct {
4948
noCopy noCopy
5049

5150
state atomic.Uint64 // high 32 bits are counter, low 32 bits are waiter count.
5251
sema uint32
5352
}
5453

55-
// Add adds delta, which may be negative, to the [WaitGroup] counter.
54+
// Add adds delta, which may be negative, to the [WaitGroup] task counter.
5655
// If the counter becomes zero, all goroutines blocked on [WaitGroup.Wait] are released.
5756
// If the counter goes negative, Add panics.
5857
//
58+
// Callers should prefer [WaitGroup.Go].
59+
//
5960
// Note that calls with a positive delta that occur when the counter is zero
6061
// must happen before a Wait. Calls with a negative delta, or calls with a
6162
// positive delta that start when the counter is greater than zero, may happen
@@ -107,12 +108,20 @@ func (wg *WaitGroup) Add(delta int) {
107108
}
108109
}
109110

110-
// Done decrements the [WaitGroup] counter by one.
111+
// Done decrements the [WaitGroup] task counter by one.
112+
// It is equivalent to Add(-1).
113+
//
114+
// Callers should prefer [WaitGroup.Go].
115+
//
116+
// In the terminology of [the Go memory model], a call to Done
117+
// "synchronizes before" the return of any Wait call that it unblocks.
118+
//
119+
// [the Go memory model]: https://go.dev/ref/mem
111120
func (wg *WaitGroup) Done() {
112121
wg.Add(-1)
113122
}
114123

115-
// Wait blocks until the [WaitGroup] counter is zero.
124+
// Wait blocks until the [WaitGroup] task counter is zero.
116125
func (wg *WaitGroup) Wait() {
117126
if race.Enabled {
118127
race.Disable()
@@ -151,7 +160,7 @@ func (wg *WaitGroup) Wait() {
151160
}
152161
}
153162

154-
// Go calls f in a new goroutine and adds that task to the WaitGroup.
163+
// Go calls f in a new goroutine and adds that task to the [WaitGroup].
155164
// When f returns, the task is removed from the WaitGroup.
156165
//
157166
// If the WaitGroup is empty, Go must happen before a [WaitGroup.Wait].
@@ -161,8 +170,10 @@ func (wg *WaitGroup) Wait() {
161170
// If a WaitGroup is reused to wait for several independent sets of tasks,
162171
// new Go calls must happen after all previous Wait calls have returned.
163172
//
164-
// In the terminology of [the Go memory model](https://go.dev/ref/mem),
165-
// the return from f "synchronizes before" the return of any Wait call that it unblocks.
173+
// In the terminology of [the Go memory model], the return from f
174+
// "synchronizes before" the return of any Wait call that it unblocks.
175+
//
176+
// [the Go memory model]: https://go.dev/ref/mem
166177
func (wg *WaitGroup) Go(f func()) {
167178
wg.Add(1)
168179
go func() {

0 commit comments

Comments
 (0)