package main import ( "fmt" "sync" ) var n int var lock sync.RWMutex func main() { wg := sync.WaitGroup{} wg.Add(1000) for i:=0; i<1000; i++ { go func() { defer wg.Done() lock.Lock() n++ lock.Unlock() }() } wg.Wait() fmt.Println(n) }
package main import ( "fmt" "sync" ) var n int var lock sync.RWMutex func main() { wg := sync.WaitGroup{} wg.Add(1000) for i:=0; i<1000; i++ { go func() { defer wg.Done() lock.Lock() n++ lock.Unlock() }() } wg.Wait() fmt.Println(n) }