跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • golang 中的 sync.WaitGroup 这样用好么
未分類
21 7 月 2020

golang 中的 sync.WaitGroup 这样用好么

golang 中的 sync.WaitGroup 这样用好么

資深大佬 : hijoker 4

把.Add()放到 goroutine 里面
golang 中的 sync.WaitGroup 这样用好么
golang 中的 sync.WaitGroup 这样用好么
大佬有話說 (8)

  • 資深大佬 : keepeye

    可以的

  • 資深大佬 : janxin

    官方文档这里说的很清楚

    Note that calls with a positive delta that occur when the counter is zero must happen before a Wait. Calls with a negative delta, or calls with a positive delta that start when the counter is greater than zero, may happen at any time. Typically this means the calls to Add should execute before the statement creating the goroutine or other event to be waited for. If a WaitGroup is reused to wait for several independent sets of events, new Add calls must happen after all previous Wait calls have returned. See the WaitGroup example.

  • 資深大佬 : sunxiansong

    如果你是希望等待所有 goroutine 完成的话,那么 wg.Add(1)最好放到 go 函数外部, defer wg.Done() 放到函数内部

  • 資深大佬 : Vegetable

    defer 没问题,但是 Add 最好放外边,虽然他在第一行,但是未必会比 wait 先执行。

  • 資深大佬 : TypeErrorNone

    不可以

  • 資深大佬 : reus

    这样用是错的,有可能没 Add,Wait 就执行了,起不到 Wait 的作用

  • 資深大佬 : sxfscool

    我觉得不行, wait()可能直接过去,一个也等不到

  • 主 資深大佬 : hijoker

    这是 minikube 的源代码, 我看到的时候也是惊呆了

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具