|
|
@@ -13,11 +13,9 @@ import (
|
|
|
)
|
|
|
|
|
|
func main() {
|
|
|
- ctx := context.Background()
|
|
|
- common.GlobalWaitGroup.SetContext(ctx)
|
|
|
test()
|
|
|
go test()
|
|
|
- common.GlobalWaitGroup.Wait()
|
|
|
+ common.GlobalWaitGroup.Stop()
|
|
|
}
|
|
|
|
|
|
func test() {
|
|
|
@@ -38,7 +36,6 @@ func test() {
|
|
|
*/
|
|
|
|
|
|
import (
|
|
|
- "context"
|
|
|
"fmt"
|
|
|
"sync"
|
|
|
"time"
|
|
|
@@ -47,37 +44,27 @@ import (
|
|
|
var GlobalWaitGroup *globalWaitGroup
|
|
|
|
|
|
func init() {
|
|
|
- GlobalWaitGroup = NewGlobalWaitGroup(context.Background())
|
|
|
+ GlobalWaitGroup = NewGlobalWaitGroup()
|
|
|
}
|
|
|
|
|
|
type globalWaitGroup struct {
|
|
|
wg sync.WaitGroup
|
|
|
- ctx context.Context
|
|
|
timeout int64 //超时时间, 时间戳
|
|
|
isStop bool
|
|
|
lock sync.Mutex
|
|
|
ok chan bool
|
|
|
}
|
|
|
|
|
|
-func NewGlobalWaitGroup(ctx context.Context) *globalWaitGroup {
|
|
|
+func NewGlobalWaitGroup() *globalWaitGroup {
|
|
|
this := &globalWaitGroup{
|
|
|
wg: sync.WaitGroup{},
|
|
|
- ctx: ctx,
|
|
|
isStop: false,
|
|
|
ok: make(chan bool, 1),
|
|
|
}
|
|
|
- go func() {
|
|
|
- <-this.ctx.Done()
|
|
|
- this.isStop = true
|
|
|
- }()
|
|
|
|
|
|
return this
|
|
|
}
|
|
|
|
|
|
-func (this *globalWaitGroup) SetContext(ctx context.Context) {
|
|
|
- this.ctx, _ = context.WithCancel(ctx)
|
|
|
-}
|
|
|
-
|
|
|
func (this *globalWaitGroup) Stop() {
|
|
|
this.isStop = true
|
|
|
this.Wait()
|