You can see my implementation here: // barrier releases callers in groups of N and makes callers wait until // there are N callers to release. // // If a caller happens to come in while the barrier is already releasing a // group N callers, that caller waits until the barrier releases the next // group of N callers. type barrier struct { inCh chan bool outCh chan bool } // newBarrier creates a new barrier. count is N. func newBarrier(count int) *barrier { result := &barrier{inCh: make(chan bool), outCh: make(chan bool)} go result.loop(count) return result } // Await blocks the caller until there are N callers to release. func (b *barrier) Await() { b.inCh <- true <-b.outCh } func (b *barrier) loop(count int) { for { for i := 0; i < count; i++ { <-b.inCh } for i := 0; i < count; i++ { b.outCh <- true } } } |