1. 首页
  2. 文章

sync.WaitGroup的使用以及坑

ironman_ ·

简单使用:

package main
import (
 "sync"
)
type httpPkg struct{}
func (httpPkg) Get(url string) {}
var http httpPkg
func main() {
 var wg sync.WaitGroup
 var urls = []string{
 "http://www.golang.org/",
 "http://www.google.com/",
 "http://www.somestupidname.com/",
 }
 for _, url := range urls {
 // Increment the WaitGroup counter.
 wg.Add(1)
 // Launch a goroutine to fetch the URL.
 go func(url string) {
 // Decrement the counter when the goroutine completes.
 defer wg.Done()
 // Fetch the URL.
 http.Get(url)
 }(url)
 }
 // Wait for all HTTP fetches to complete.
 wg.Wait()
}

这是golang官网上的例子

跟java的CountdownLatch差不多,也是阻塞等待所有任务完成之后再继续执行。

简单使用就是在创建一个任务的时候wg.Add(1), 任务完成的时候使用wg.Done()来将任务减一。使用wg.Wait()来阻塞等待所有任务完成。

然后我就写了一个例子:

func main() {
 var wg sync.WaitGroup
 ch := make(chan int, 1000)
 for i := 0; i < 1000; i++ {
 wg.Add(1)
 go doSomething(i, wg, ch)
 }
 wg.Wait()
 fmt.Println("all done")
 for i := 0; i < 1000; i++ {
 dd := <-ch
 fmt.Println("from ch:"+strconv.Itoa(dd))
 }
}
func doSomething(index int, wg sync.WaitGroup, ch chan int) {
 defer wg.Done()
 fmt.Println("start done:" + strconv.Itoa(index))
 //time.Sleep(20 * time.Millisecond)
 ch <- index
}

然后就报错了:

fatal error: all goroutines are asleep - deadlock!
goroutine 1 [semacquire]:
sync.runtime_Semacquire(0xc42001608c)
 /usr/local/Cellar/go/1.10.3/libexec/src/runtime/sema.go:56 +0x39
sync.(*WaitGroup).Wait(0xc420016080)
 /usr/local/Cellar/go/1.10.3/libexec/src/sync/waitgroup.go:129 +0x72
main.main()
 /Users/taoli/go/src/github.com/c60/cai/tx_gorutine.go:16 +0xea

怎么回事,他说死锁了。
原来这是说,所有的协程都运行完了,你这边还在等待。

什么原因导致的呢?
原来是golang里如果方法传递的不是地址,那么就会做一个拷贝,所以这里调用的wg根本就不是一个对象。

传递的地方传递地址就可以了:

go doSomething(i, &wg, ch)
func doSomething(index int, wg *sync.WaitGroup, ch chan int) {

本文来自:简书

感谢作者:ironman_

查看原文:sync.WaitGroup的使用以及坑

用户登录

没有账号?注册

今日阅读排行

    加载中

一周阅读排行

    加载中