* T L <tapir....@gmail.com> [170707 01:32]:
> Then how about this?
> 
> package main
> 
> import "fmt"
> import "sync/atomic"
> 
> func main() {
>     var x, y int32
>     go func() {
>         atomic.AddInt32(&x, 1)
>         atomic.AddInt32(&y, 1)
>     }()
>     
>     if atomic.LoadInt32(&y) == 1 {
>         fmt.Println("x =", atomic.LoadInt32(&x)) // always 1 if it is printed?
>     }

Yes, the go routine establishes a happens-before relationship such that
x is incremented before y, and the atomic Add of y and Load of y ensure
that the Load either happens before or after, but not during, the Add.

>     if atomic.LoadInt32(&x) == 0 {
>         fmt.Println("y =", atomic.LoadInt32(&y)) // always 0 if it is printed?
>     }

No.  If the Load of x returns 0, it must happen before the Add of 1 to x.
But after the test and before the Load of y, the main go routine and the
func go routine are both runnable and there is nothing to establish a
happens-before relationship between the Load of y and the Add of 1 to y.

> }

...Marvin

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to