欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

golang中的WaitGroup

发布时间:2025/6/15 52 豆豆
生活随笔 收集整理的这篇文章主要介绍了 golang中的WaitGroup 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

简介

不要把增加其计数器值的操作和调用其Wait方法的代码,放在不同的 goroutine 中执行。换句话说,要杜绝对同一个WaitGroup值的两种操作的并发执行

WaitGroup的用途:它能够一直等到所有的goroutine执行完成,并且阻塞主线程的执行,直到所有的goroutine执行完成。

sync.WaitGroup只有3个方法,Add(),Done(),Wait()。

其中Done()是Add(-1)的别名。简单的来说,使用Add()添加计数,Done()减掉一个计数,计数不为0, 阻塞Wait()的运行。

也可以通过这种方式创建

wg := new(sync.WaitGroup)
  • Add 用来添加 goroutine 的个数

  • Done 执行一次数量减 1

  • Wait 用来等待结束

例子

同时开三个协程去请求网页, 等三个请求都完成后才继续 Wait 之后的工作

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()

或者下面的代码
用于测试 给chan发送 1千万次,并接受1千万次的性能

import ("fmt""sync""time" )const (num = 10000000 )func main() {TestFunc("testchan", TestChan) }func TestFunc(name string, f func()) {st := time.Now().UnixNano()f()fmt.Printf("task %s cost %d \r\n", name, (time.Now().UnixNano()-st)/int64(time.Millisecond)) }func TestChan() {var wg sync.WaitGroupc := make(chan string)wg.Add(1)go func() {for _ = range c {}wg.Done()}()for i := 0; i < num; i++ {c <- "123"}close(c)wg.Wait() }

总结

以上是生活随笔为你收集整理的golang中的WaitGroup的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。