Skip to content

Instantly share code, notes, and snippets.

@minhphong306
Last active April 19, 2020 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minhphong306/f8794058cb0d64329fe4945aa5b6a99b to your computer and use it in GitHub Desktop.
Save minhphong306/f8794058cb0d64329fe4945aa5b6a99b to your computer and use it in GitHub Desktop.
vi_du_wait_group.go
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