forked from all_user/stat_center
Initial commit
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
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()
|
||||
}
|
||||
Reference in New Issue
Block a user