github.com/pingcap/chaos@v0.0.0-20190710112158-c86faf4b3719/pkg/util/sync.go (about) 1 package util 2 3 import "sync" 4 5 // BlockRunner provides a simple way to run tasks, 6 // block until all the tasks are finished. 7 type BlockRunner struct { 8 once sync.Once 9 wg sync.WaitGroup 10 } 11 12 // Init initializes how many tasks we want to run synchronously. 13 func (r *BlockRunner) Init(n int) { 14 r.once.Do(func() { 15 r.wg.Add(n) 16 }) 17 } 18 19 // Run runs the task in different goroutines and 20 // block until all the tasks are finished. 21 func (r *BlockRunner) Run(f func()) { 22 f() 23 r.wg.Done() 24 r.wg.Wait() 25 }