Atomics

Theory

Atomic addition and subtraction

Report a typo

What will be the final output in this code snippet?

func main() {
    var counter int32
    var wg sync.WaitGroup
    wg.Add(3)

    go func() {
        atomic.AddInt32(&counter, 10)
        wg.Done()
    }()
    go func() {
        atomic.AddInt32(&counter, 20)
        wg.Done()
    }()
    go func() {
        atomic.AddInt32(&counter, -5)
        wg.Done()
    }()
    wg.Wait()

    fmt.Println("Final Counter: ", counter)
}
Enter a short text

Create a free account to access the full topic