gitee.com/gricks/utils@v1.0.8/parallel.go (about)

     1  package utils
     2  
     3  import "sync"
     4  
     5  type Parallel struct {
     6  	sig chan struct{}
     7  	wg  sync.WaitGroup
     8  }
     9  
    10  func NewParallel(n int) *Parallel {
    11  	return &Parallel{
    12  		sig: make(chan struct{}, n),
    13  	}
    14  }
    15  
    16  func (p *Parallel) Add(n int) {
    17  	p.wg.Add(n)
    18  	p.sig <- struct{}{}
    19  }
    20  
    21  func (p *Parallel) Done() {
    22  	<-p.sig
    23  	p.wg.Done()
    24  }
    25  
    26  func (p *Parallel) Wait() {
    27  	p.wg.Wait()
    28  }