Add atomic
This commit is contained in:
parent
b72ebaf843
commit
c23e699122
30
atomics/atomics.go
Normal file
30
atomics/atomics.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
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())
|
||||||
|
}
|
6
main.go
6
main.go
@ -14,7 +14,8 @@ package main
|
|||||||
// import "git.sangeeth.dev/gobyexample/tickers"
|
// import "git.sangeeth.dev/gobyexample/tickers"
|
||||||
// import "git.sangeeth.dev/gobyexample/workerpools"
|
// import "git.sangeeth.dev/gobyexample/workerpools"
|
||||||
// import "git.sangeeth.dev/gobyexample/waitgroups"
|
// import "git.sangeeth.dev/gobyexample/waitgroups"
|
||||||
import "git.sangeeth.dev/gobyexample/ratelimiting"
|
// import "git.sangeeth.dev/gobyexample/ratelimiting"
|
||||||
|
import "git.sangeeth.dev/gobyexample/atomics"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// runes.Runes()
|
// runes.Runes()
|
||||||
@ -31,5 +32,6 @@ func main() {
|
|||||||
// tickers.Tickers()
|
// tickers.Tickers()
|
||||||
// workerpools.WorkerPools()
|
// workerpools.WorkerPools()
|
||||||
// waitgroups.WaitGroups()
|
// waitgroups.WaitGroups()
|
||||||
ratelimiting.RateLimiting()
|
// ratelimiting.RateLimiting()
|
||||||
|
atomics.Atomics()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user