mirror of
https://github.com/alrayyes/wiki.git
synced 2024-11-22 19:46:23 +00:00
53 lines
950 B
Markdown
53 lines
950 B
Markdown
---
|
|
id: 53c0ad13-0c19-4a84-9c67-cc31d036be4d
|
|
title: Golang WaitGroup
|
|
---
|
|
|
|
# Description
|
|
|
|
A [WaitGroup](https://golang.org/pkg/sync/#WaitGroup) waits for a
|
|
collection of goroutines to finish. The main goroutine calls Add to set
|
|
the number of goroutines to wait for. Then each of the goroutines runs
|
|
and calls Done when finished. At the same time, Wait can be used to
|
|
block until all goroutines have finished.
|
|
|
|
A WaitGroup must not be copied after first use.
|
|
|
|
# Syntax
|
|
|
|
``` go
|
|
type Counter struct {
|
|
mu sync.Mutex
|
|
value int
|
|
}
|
|
|
|
func (c *Counter) Inc() {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
|
|
c.value++
|
|
}
|
|
|
|
func (c *Counter) Value() int {
|
|
return c.value
|
|
}
|
|
|
|
func NewCounter() *Counter {
|
|
return &Counter{}
|
|
}
|
|
|
|
wantedCount := 1000
|
|
counter := NewCounter()
|
|
|
|
var wg sync.WaitGroup
|
|
wg.Add(wantedCount)
|
|
|
|
for i := 0; i < wantedCount; i++ {
|
|
go func(w *sync.WaitGroup) {
|
|
counter.Inc()
|
|
w.Done()
|
|
}(&wg)
|
|
}
|
|
|
|
wg.Wait()
|
|
```
|