Surely single goroutine scenarios are trivial? Anything else would be a compiler bug. In multiple goroutine scenarios a lock must be held or some of the atomic functions are needed. Any other use that "works" are by accident say on x86 or some other stricter arch. I am no expert in this but surely hope this is true because much of my code depends on it...
On Fri, Dec 29, 2017, 09:57 Caleb Spare <cesp...@gmail.com> wrote: > > I believe that program will always print 2, however when you bring > multiple Goroutines into the mix, I’m less sure. > > I'm using examples without goroutines because the crux of our > discussion, as I understand it, is whether a plain read ('if o.done == > 0') will read the result of an atomic write > ('atomic.StoreUint32(&o.done, 1)') given that there is a > happens-before relationship between them. > > The simplest kind of happens-before relationship is that between > sequential lines of straight-line code in a single goroutine. If you > agree that the mutexes imply a happens-before relationship between the > atomic write and the plain read, then we can simplify the discussion > by only considering sequential code in a single goroutine. > > Regarding the sequential, single-goroutine example code: for my first > example with atomic.StoreUint32 > (https://play.golang.org/p/fdf8VeJ98Jv), you wrote > > > I agree it should print 2, but my reading of the memory model and > > https://github.com/golang/go/issues/5045 doesn't give me any concrete > > guarantee that it must. This is why i'm not sure about the > > interactions between atomic and non atomic loads. > > but for the second example with a non-atomic store function > (https://play.golang.org/p/gk3c6X_JdmQ), you wrote > > > I believe that program will always print 2 > > What's the distinction you see between them? > > -- > 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.