package main
import("fmt""sync""sync/atomic")funcatomicAdd(){var data, data2 int64var wg sync.WaitGroup
for i :=0; i <1000; i++{
wg.Add(1)gofunc(){
atomic.AddInt64(&data,1)
data2++
wg.Done()}()}
wg.Wait()
fmt.Println("data", data,"data2", data2)}// From medium: https://medium.com/a-journey-with-go/go-how-to-reduce-lock-contention-with-the-atomic-package-ba3b2664b549type Config struct{
a []int}// 用 go run -race main.go atomic.go 观察data racesfuncdataRaces(){
cfg :=&Config{}gofunc(){
i :=0for{
i++
cfg.a =[]int{i, i +1, i +2, i +3, i +4, i +5}}}()var wg sync.WaitGroup
for n :=0; n <4; n++{
wg.Add(1)gofunc(){for n :=0; n <100; n++{
fmt.Printf("%v\n", cfg)}
wg.Done()}()}
wg.Wait()}funcdataRacesWithLock(){var mu sync.RWMutex
cfg :=&Config{}gofunc(){
i :=0for{
i++
mu.Lock()
cfg.a =[]int{i, i +1, i +2, i +3, i +4, i +5}
mu.Unlock()}}()var wg sync.WaitGroup
for n :=0; n <4; n++{
wg.Add(1)gofunc(){for n :=0; n <100; n++{
mu.RLock()
fmt.Printf("%v\n", cfg)
mu.RUnlock()}
wg.Done()}()}
wg.Wait()}funcdataRacesWithAtomic(){var v atomic.Value
gofunc(){
i :=0for{
i++
cfg :=&Config{a:[]int{i, i +1, i +2, i +3, i +4, i +5}}
v.Store(cfg)}}()var wg sync.WaitGroup
for n :=0; n <4; n++{
wg.Add(1)gofunc(){for n :=0; n <100; n++{
cfg := v.Load()
fmt.Printf("%v\n", cfg)}
wg.Done()}()}
wg.Wait()}// cas相关的代码,建议直接参考源码中的相关实现