Add atomic

This commit is contained in:
Sangeeth Sudheer 2024-05-01 16:01:47 +05:30
parent b72ebaf843
commit c23e699122
Signed by: x
GPG Key ID: F6D06ECE734C57D1
2 changed files with 34 additions and 2 deletions

30
atomics/atomics.go Normal file
View 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())
}

View File

@ -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()
} }