Files
stat_center/common/utils/sync.go
T
2026-05-20 16:13:58 +08:00

45 lines
677 B
Go

package utils
import (
"fmt"
"sync"
)
type WaitGroup struct {
wg sync.WaitGroup
fs []func()
}
func NewWaitGroup() *WaitGroup {
wg := &WaitGroup{}
wg.fs = make([]func(), 0)
return wg
}
func (w *WaitGroup) Add(f ...func()) *WaitGroup {
if w == nil {
panic("WaitGroup can not be null\n")
}
if w.fs == nil {
panic("WaitGroup may be not init\n")
}
w.fs = append(w.fs, f...)
return w
}
func (w *WaitGroup) RunWaits() {
w.wg.Add(len(w.fs))
for _, f := range w.fs {
go func(wg *sync.WaitGroup, fn func()) {
defer func() {
if err := recover(); err != nil {
fmt.Printf("%v\n", err)
}
wg.Done()
}()
fn()
}(&w.wg, f)
}
w.wg.Wait()
}