Go实例: Atomic Counters

Dabria ·
更新时间:2024-09-20
· 967 次阅读

package main import ( "fmt" "sync" "sync/atomic" ) func main() { var ops uint64 var wg sync.WaitGroup for i := 0; i < 50; i++ { wg.Add(1) go func() { for c := 0; c < 1000; c++ { atomic.AddUint64(&ops, 1) } wg.Done() }() } wg.Wait() fmt.Println("ops:", ops) }$ go run atomic-counters.go ops: 50000



GO实例 atomic GO

需要 登录 后方可回复, 如果你还没有账号请 注册新账号