func main() { counter := 0 var wg sync.WaitGroup wg.Add(100) for j := 0; j < 100 ; j++{ go func() { counter += 1 wg.Done() }() } wg.Wait() fmt.Println(counter)
var wg1 sync.WaitGroup var mt1 sync.Mutex var counter1 int wg1.Add(100) for i := 0; i < 100; i++ { go func(i int) { mt1.Lock() counter1++ mt1.Unlock() wg1.Done() }(i) } wg1.Wait() fmt.Println(counter1)
var wg2 sync.WaitGroup var mt2 sync.RWMutex var counter2 int wg2.Add(100) for i := 0; i < 100; i++ { go func(i int) { mt2.RLock() counter2++ mt2.RUnlock() wg2.Done() }(i) } wg2.Wait() fmt.Println(counter2) }