atomic counter
atomic counter
原文:https://gobyexample.com/atomic-counters
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| package main
import (
"fmt"
"sync"
"sync/atomic"
)
func main() {
var counter uint64
var wg sync.WaitGroup
for i := 0; i < 50; i++ {
wg.Add(1)
go func() {
for j := 0; j < 1000; j++ {
atomic.AddUint64(&counter, 1)
}
wg.Done()
}()
}
wg.Wait()
fmt.Println("counter:", counter)
}
|