I'm not an expert, but I think this is an invalid oversimplification. Say
you have three goroutines:

var x, y int
ch := make chan int
go func() {
    x = 1
    <-ch
    y = 1
}()
go func() {
    close(ch)
}()
a, b := x, y

>From my understanding of the memory model, it would be legal to get a, b =
0, 1 (thus, the write of y was observed before the write of x).
The reason is, that the memory model only makes guarantees for the ordering
of effects of goroutine A observed by goroutine B, if there is a
happens-before edge *between these two goroutines*. The assignments to a
resp. b have no happens-before edges with the assignments of x or y.

So, any formulation of the memory-model guarantees necessarily have to talk
about relationships between multiple goroutines and their statements.

But I'm not an expert. I consider all of this memory-model,
happens-before-relationship arguing to be incredibly tedious. Which is why
I just a) add synchronization points when I need to concurrently access
data, b) just in general rather lock too much than too little and  c) hope
that the race detector can tell me when I'm wrong. Rules-lawyering, it
would seem to me, is not a terribly promising strategy.


On Fri, Oct 27, 2017 at 10:39 PM, T L <tapir....@gmail.com> wrote:

> Go 1 memory model guarantee that the relative order of the two statements
> will not get exchanged,
> if there is one any of the following operation between the two statements
> in code:
> * a channel read
> * a channel write
> * a channel close
> * a sync.Mutex.Lock()
> * a sync.Mutex.Unlock()
>
> --
> 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.
>

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