31 lines
345 B
Go
31 lines
345 B
Go
package atomics
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
)
|
|
|
|
func Atomics() {
|
|
var counter atomic.Uint64
|
|
var wg sync.WaitGroup
|
|
|
|
for range 50 {
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
time.Sleep(100 * time.Millisecond)
|
|
for range 1000 {
|
|
counter.Add(1)
|
|
}
|
|
|
|
wg.Done()
|
|
}()
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
fmt.Println("Final tally is", counter.Load())
|
|
}
|