如何获取与WaitGroup关联的goroutine数量?

huangapple go评论101阅读模式
英文:

How to get the number of goroutines associated with a WaitGroup?

问题

假设我使用WaitGroup来使应用程序的主线程等待,直到我从该主线程启动的所有goroutine都完成。

有没有一种安全、直接的方法,在任何时间点上评估与该WaitGroup相关联的goroutine中还有多少正在运行的?

英文:

Let’s say I use a WaitGroup to make the main thread of an application wait until all the goroutines I have launched from said main have completed.

Is there a safe, straightforward, way to assess at any point in time how many goroutines associated with said WaitGroup are still running?

答案1

得分: 17

WaitGroup的内部状态是不可见的,并且也不会被公开:https://github.com/golang/go/issues/7202

我认为我们不太可能使这个API更复杂。除了简单地打印它们之外,我没有看到任何不容易出现竞态条件的使用计数器和等待者的方法。而且你可以自己维护计数。

你可以自己实现一个计数器:

type WaitGroupCount struct {
    sync.WaitGroup
    count int64
}

func (wg *WaitGroupCount) Add(delta int) {
    atomic.AddInt64(&wg.count, int64(delta))
    wg.WaitGroup.Add(delta)
}

func (wg *WaitGroupCount) Done() {
    atomic.AddInt64(&wg.count, -1)
    wg.WaitGroup.Done()
}

func (wg *WaitGroupCount) GetCount() int {
    return int(atomic.LoadInt64(&wg.count))
}

// Wait() 从嵌入字段中提升

然而,即使计数器的访问是同步的,在你读取它之后它会立即变得过时,因为其他goroutine可能会继续调用AddDone,而不管你对计数做了什么操作。除非你同步依赖于计数的整个操作,否则你可能需要一个更复杂的数据结构。

英文:

The internal state of the WaitGroup is not exposed, and it won't be: https://github.com/golang/go/issues/7202

> I don't think we're likely to make this API more complex. I don't see any way to use
counter and waiters that is not subject to race conditions, other than simply printing
them out. And for that you can maintain your own counts.

You could implement a counter yourself:

type WaitGroupCount struct {
    sync.WaitGroup
    count int64
}

func (wg *WaitGroupCount) Add(delta int) {
    atomic.AddInt64(&wg.count, int64(delta))
    wg.WaitGroup.Add(delta)
}

func (wg *WaitGroupCount) Done() {
    atomic.AddInt64(&wg.count, -1)
    wg.WaitGroup.Done()
}

func (wg *WaitGroupCount) GetCount() int {
    return int(atomic.LoadInt64(&wg.count))
}

// Wait() promoted from the embedded field

However, even if the counter access is synchronized, it will become stale immediately after you read it, since other goroutines may go on and call Add or Done irrespective of what you are doing with the count — unless you synchronize the entire operation that depends on the count. But in that case, you might need a more complex data structure altogether.

答案2

得分: 3

在任何时间点上,有没有一种安全、简单的方法来评估与所述等待组相关联的 goroutine 的运行情况?

没有。

这是因为 Go 语言本身没有“与等待组相关联的 goroutine”的概念。

英文:

> Is there a safe, straightforward, way to assess at any point in time how many goroutines associated with said waitgroup are still running?

No, there isn't.

Simply because Go (the language) has no notion of "goroutines associated with [a] waitgroup".

huangapple
  • 本文由 发表于 2021年8月31日 16:15:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/68995144.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定