实现自定义 wg 以统计协程数量

This commit is contained in:
2025-02-25 15:44:09 +08:00
parent 7f23e2741f
commit 9a8680a221
4 changed files with 92 additions and 64 deletions

29
pkg/utils/sync.go Normal file
View File

@@ -0,0 +1,29 @@
package utils
import (
"sync"
"sync/atomic"
)
type CountWaitGroup struct {
wg sync.WaitGroup
num atomic.Uint64
}
func (c *CountWaitGroup) Add(delta uint64) {
c.wg.Add(int(delta))
c.num.Add(delta)
}
func (c *CountWaitGroup) Done() {
c.wg.Done()
c.num.Add(-1)
}
func (c *CountWaitGroup) Wait() {
c.wg.Wait()
}
func (c *CountWaitGroup) Count() uint64 {
return c.num.Load()
}