go用chan实现WaitGroup并支持超时

来来来,话不多说,上代码

package main

import "fmt"
import "time"
import "sync"

type group struct {
	gc chan bool
	tk *time.Ticker
	cap int
	mutex sync.Mutex
}

func WaitGroup(timeOuteRec int) *group{
	timeout     := time.Millisecond * time.Duration(timeOuteRec)
	wg := group{
		gc   : make(chan bool),
		cap  :  0,
		tk   : time.NewTicker(timeout),
	}

	return &wg
}


func(w *group)Add(index int){
	w.mutex.Lock()
	w.cap++
	w.mutex.Unlock()

	go func(w *group,index int) {
		for i := 0;i

 

 

 

 

你可能感兴趣的:(GoLang)