package main import ( "log" "sync" ) type IBarrier interface { Await() error } type Barrier struct { cond *sync.Cond gos uint curgos uint } func NewBarrier(syncGos uint)*Barrier{ if syncGos < 1{ panic("min 1") } l := &sync.Mutex{} c := sync.NewCond(l) return &Barrier{ cond:c, gos:syncGos, } } func (b *Barrier)Await() error{ b.cond.L.Lock() defer b.cond.L.Unlock() b.curgos++ if b.gos != b.curgos{ b.cond.Wait() }else{ b.curgos = 0 b.cond.Broadcast() } return nil } func main(){ var b IBarrier wg := &sync.WaitGroup{} gos := 10 wg.Add(gos) b = NewBarrier(uint(gos)) for i:=0;i