I know it can happen that g prints 2 and then 0 given the following code.
var a, b uint32
func f() {
a = 1
b = 2
}
func g() {
fmt.Println(b)
fmt.Println(a)
}
func main() {
go f()
g()
}
What if I change all read and write to atomic operations? Is that guaranteed that if g prints 2 first, then 1 is also printed?
var a, b uint32
func f() {
atomic.StoreUint32(&a, 1)
atomic.StoreUint32(&b, 2)
}
func g() {
fmt.Println(atomic.LoadUint32(&b))
fmt.Println(atomic.LoadUint32(&a))
}
func main() {
go f()
g()
}
f()
won't be executed at all. – Litre