Preface
If you have a task that can be decomposed into multiple subtasks for processing, and there is no restriction on the order of execution of each subtask, wait until all subtasks are executed before proceeding to the next step. At this time, the execution of each subtask can be processed concurrently. In this scenario, sync.WaitGroup
is suitable.
Although sync.WaitGroup
relatively simple to use, it is very likely to step in the pit if you carelessly.
Use sync.WaitGroup correctly
For example, if there is a task that needs to perform 3 subtasks, then you can write:
func main() {
var wg sync.WaitGroup
wg.Add(3)
go handlerTask1(&wg)
go handlerTask2(&wg)
go handlerTask3(&wg)
wg.Wait()
fmt.Println("全部任务执行完毕.")
}
func handlerTask1(wg *sync.WaitGroup) {
defer wg.Done()
fmt.Println("执行任务 1")
}
func handlerTask2(wg *sync.WaitGroup) {
defer wg.Done()
fmt.Println("执行任务 2")
}
func handlerTask3(wg *sync.WaitGroup) {
defer wg.Done()
fmt.Println("执行任务 3")
}
Execution output:
执行任务 3
执行任务 1
执行任务 2
全部任务执行完毕.
Sync.WaitGroup closed pit guide
01
// 正确
go handlerTask1(&wg)
// 错误
go handlerTask1(wg)
When performing subtasks, the sync.WaitGroup
must be the reference type of wg
02
Be careful not to put wg.Add()
in go handlerTask1(&wg)
!
E.g:
// 错误
var wg sync.WaitGroup
go handlerTask1(&wg)
wg.Wait()
...
func handlerTask1(wg *sync.WaitGroup) {
wg.Add(1)
defer wg.Done()
fmt.Println("执行任务 1")
}
Note that wg.Add()
must be executed before wg.Wait()
03
Note that the counters of wg.Add()
and wg.Done()
In fact, wg.Done()
is the execution of wg.Add(-1)
.
summary
sync.WaitGroup
relatively simple to use, so be careful not to step in the pit.
In fact, the sync.WaitGroup
more limited. It is only suitable for waiting for all subtasks to be executed before proceeding to the next step. If the requirement is to notify other subtasks to stop running when the first subtask fails to execute, then sync.WaitGroup
cannot be satisfied. channel
, you need to use the notification mechanism (061863d13b8d5d).
The above, I hope it can help you.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。