Skip to content

Commit fbd88ae

Browse files
committed
Update use-sync-waitgroup-to-synchronize-goroutines.md.
1 parent 34d67f6 commit fbd88ae

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

posts/use-sync-waitgroup-to-synchronize-goroutines.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Use sync.WaitGroup to synchronize goroutines
22
----
3+
(This post is a modification edition of [Use sync.WaitGroup in Golang](http://nanxiao.me/en/use-sync-waitgroup-in-golang/)).
4+
35
[sync.WaitGroup](https://golang.org/pkg/sync/#WaitGroup) provides a goroutine synchronization mechanism, and used for waiting for a collection of goroutines to finish. In the internal of `sync.WaitGroup` struct, there is a `counter` which records how many goroutines need to be waited are living now.
46

57
`sync.WaitGroup` provides `3` methods: `Add`, `Done` and `Wait`. `Add` method is used to identify how many goroutines need to be waited, and it will add `counter` value. When a goroutine exits, it must call `Done`, and it will decrease `counter` value by `1`. The `main` goroutine blocks on `Wait`, once the `counter` becomes `0`, the `Wait` will return, and main goroutine can continue to run.
@@ -35,7 +37,4 @@ Because the `main` goroutine need to wait `2` goroutines, so the argument for `w
3537
goroutine exit
3638
goroutine exit
3739
Main goroutine exit
38-
Please notice, the `Add` must go ahead of `Done`.
39-
40-
Reference:
41-
[Use sync.WaitGroup in Golang](http://nanxiao.me/en/use-sync-waitgroup-in-golang/).
40+

0 commit comments

Comments
 (0)