gobyexample/atomics/atomics.go

31 lines
345 B
Go
Raw Normal View History

2024-05-01 10:31:47 +00:00
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())
}