代码拉取完成,页面将自动刷新
package main
// Here we'll look at using the sync/atomic package for
// atomic counters accessed by multiple goroutines.
import (
"fmt"
"sync"
"sync/atomic"
)
func main() {
// We'll use an atomic integer type to represent our counter
var ops atomic.Uint64
// A WaitGroup will help us wait for all goroutines to finish their work.
var wg sync.WaitGroup
// We'll start 50 goroutines that each increment the counter exactly 1000 times
for i := 0; i < 50; i++ {
wg.Add(1)
go func() {
for c := 0; c < 1000; c++ {
ops.Add(1)
}
wg.Done()
}()
}
// Wait until all the goroutines are done
wg.Wait()
// Here no goroutines are writing to 'ops', but using Load it's safe
// to atomically read a value even while other goroutines are atomically
// updating it.
fmt.Println("ops:", ops.Load())
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。