I have implemented atomic.LoadInt64 in a way for what I am expecting it will print value in serial order. But it is printing random value. What’s wrong here?
Since I do not understand atomic in deep so what is the concept I am missing here to implement it correctly?
package main
import (
"fmt"
"sync"
"sync/atomic"
)
var wg sync.WaitGroup
var counter int64
func concurrencyGenerator() {
gs := 1000
wg.Add(gs)
for i := 0; i < gs; i++ {
go func() {
atomic.AddInt64(&counter, 1)
fmt.Println("Value : ", atomic.LoadInt64(&counter))
wg.Done()
}()
}
}
func main() {
concurrencyGenerator()
wg.Wait()
fmt.Println(counter)
}