mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-24 01:30:25 +08:00
38 lines
673 B
Go
38 lines
673 B
Go
|
package threading
|
||
|
|
||
|
import "sync"
|
||
|
|
||
|
type RoutineGroup struct {
|
||
|
waitGroup sync.WaitGroup
|
||
|
}
|
||
|
|
||
|
func NewRoutineGroup() *RoutineGroup {
|
||
|
return new(RoutineGroup)
|
||
|
}
|
||
|
|
||
|
// Don't reference the variables from outside,
|
||
|
// because outside variables can be changed by other goroutines
|
||
|
func (g *RoutineGroup) Run(fn func()) {
|
||
|
g.waitGroup.Add(1)
|
||
|
|
||
|
go func() {
|
||
|
defer g.waitGroup.Done()
|
||
|
fn()
|
||
|
}()
|
||
|
}
|
||
|
|
||
|
// Don't reference the variables from outside,
|
||
|
// because outside variables can be changed by other goroutines
|
||
|
func (g *RoutineGroup) RunSafe(fn func()) {
|
||
|
g.waitGroup.Add(1)
|
||
|
|
||
|
GoSafe(func() {
|
||
|
defer g.waitGroup.Done()
|
||
|
fn()
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func (g *RoutineGroup) Wait() {
|
||
|
g.waitGroup.Wait()
|
||
|
}
|