Last active
April 19, 2020 15:34
-
-
Save minhphong306/f8794058cb0d64329fe4945aa5b6a99b to your computer and use it in GitHub Desktop.
vi_du_wait_group.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"sync" | |
"time" | |
) | |
func test1(wg *sync.WaitGroup) { | |
fmt.Println("Chạy hàm test 1") | |
// Oánh dấu đã done | |
wg.Done() | |
} | |
func test2(wg *sync.WaitGroup) { | |
fmt.Println("Chạy hàm test 2") | |
// Oánh dấu đã done | |
wg.Done() | |
} | |
func main() { | |
// Khai báo 1 wait group | |
var wg sync.WaitGroup | |
// Set giá trị counter = 2 | |
wg.Add(2) | |
go test1(&wg) | |
go test2(&wg) | |
// Block main goroutines chạy tiếp, khi nào counter = 0 thì mới cho chạy q | |
wg.Wait() | |
fmt.Println("Good bye") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment