github.com/songzhibin97/gkit@v1.2.13/wgroup/wgroup.go (about) 1 package wgroup 2 3 import ( 4 "context" 5 "sync" 6 7 "github.com/songzhibin97/gkit/goroutine" 8 ) 9 10 type Group struct { 11 wg sync.WaitGroup 12 goroutine goroutine.GGroup 13 } 14 15 func (g *Group) Wait() { 16 g.wg.Wait() 17 } 18 19 func (g *Group) AddTask(f func()) bool { 20 g.wg.Add(1) 21 return g.goroutine.AddTask(func() { 22 defer g.wg.Done() 23 f() 24 }) 25 } 26 27 func (g *Group) AddTaskN(ctx context.Context, f func()) bool { 28 g.wg.Add(1) 29 return g.goroutine.AddTaskN(ctx, func() { 30 defer g.wg.Done() 31 f() 32 }) 33 } 34 35 func WithContextGroup(group goroutine.GGroup) *Group { 36 g := &Group{} 37 g.goroutine = group 38 return g 39 } 40 41 // WithContext 实例化方法 42 func WithContext(ctx context.Context) *Group { 43 g := &Group{} 44 g.goroutine = goroutine.NewGoroutine(ctx) 45 return g 46 }