对int类型进行i++自增操作时并不是原子操作,并发不安全
package main import ( "fmt" "runtime" "sync" ) func main() { runtime.GOMAXPROCS(5) var m int32 = 0 var group sync.WaitGroup for i := 0; i < 100000; i++ { group.Add(1) go func() { defer func() { group.Done() }() // atomic.AddInt32(&m, 1) m++ // fmt.Println(m) }() } group.Wait() fmt.Print(m) }