On Monday, July 10, 2017 at 8:36:54 PM UTC+8, Jan Mercl wrote:
>
> On Mon, Jul 10, 2017 at 2:24 PM T L <tapi...@gmail.com <javascript:>> 
> wrote:
>
> > Aha, what I wanted to express is the execution orders of the two lines 
> may be randomized.
> >
> > atomic.AddInt32(&x, 1) 
> > atomic.AddInt32(&y, 1) 
>
> No, assuming those two lines are as written, ie. part of single function 
> and thus executed in a single/the same goroutine. Then the memory model 
> guarantees they are executed in order, ie. x will be updated before y. But 
> there's no such guarantee when the same variable x and y are observed in a 
> different, concurrently executing goroutine. To establish such ordering you 
> have to synchronize, only that gives you the H-B relation within the 
> synchronization participants.
>
> -- 
>
> -j
>

so this is guaranteed by go memory model?

package main

import "fmt"
import "sync/atomic"

func main() {
    var x, y int32
    
    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?
    }
} 

-- 
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