Go WaitGroup: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
Line 1: | Line 1: | ||
=External= | |||
* https://pkg.go.dev/sync#WaitGroup | |||
=Internal= | =Internal= | ||
* [[Go_Package_sync#WaitGroup|The <tt>sync</tt> package]] | * [[Go_Package_sync#WaitGroup|The <tt>sync</tt> package]] | ||
* [[Go_Language_Goroutines#WaitGroup|Goroutines]] | * [[Go_Language_Goroutines#WaitGroup|Goroutines]] | ||
= | =Overview= | ||
A <code>WaitGroup</code> is a counting semaphore that waits for a set of goroutines to finish. The main goroutine calls <code>Add()</code> to set the number of goroutines to wait for. When each of the counted goroutines runs, they should call <code>Done()</code> when finished. At the same time, the main goroutine blocks on <code>Wait()</code> until all other goroutines have finished. | |||
A < | |||
<blockquote style="background-color: AliceBlue; border: solid thin LightSteelBlue;"> | <blockquote style="background-color: AliceBlue; border: solid thin LightSteelBlue;"> | ||
:Using a <tt>waitGroup</tt> to wait for goroutines to finish before exiting <tt>main()</tt>: [https://github.com/NovaOrdis/playground/blob/master/go/concurrency/waitGroup.go playground/go/concurrency/waitGroup.go] | :Using a <tt>waitGroup</tt> to wait for goroutines to finish before exiting <tt>main()</tt>: [https://github.com/NovaOrdis/playground/blob/master/go/concurrency/waitGroup.go playground/go/concurrency/waitGroup.go] | ||
</blockquote> | </blockquote> |
Revision as of 21:14, 1 September 2023
External
Internal
Overview
A WaitGroup
is a counting semaphore that waits for a set of goroutines to finish. The main goroutine calls Add()
to set the number of goroutines to wait for. When each of the counted goroutines runs, they should call Done()
when finished. At the same time, the main goroutine blocks on Wait()
until all other goroutines have finished.
- Using a waitGroup to wait for goroutines to finish before exiting main(): playground/go/concurrency/waitGroup.go