On Sat, Jan 21, 2023 at 10:36 AM Peter Rabbitson <ribasu...@gmail.com> wrote:
> Greetings, > > I am trying to understand the exact mechanics of memory write ordering > from within the same goroutine. I wrote a self-contained runnable example > with the question inlined here: https://go.dev/play/p/ZXMg_Qq3ygF and am > copying its header here: > > // Below is a complete example, with the question starting on line 38: > // how do I ensure that a *separate Linux OS process* observing `IPCfile` > // (either via pread() or mmap()) can *NEVER* observe W2 before W1. > // The only permissible states are: > // 1. no changes visible > // 2. only W1 is visible > // 3. both W1 and W2 are visible > This is based on my interpretation of the go memory model: Atomic memory operations are sequentially consistent, so here: (*mmapBufAtomic.Load())[fSize-1] = 255 // W1 (*mmapBufAtomic.Load())[0] = 42 // W2 The first atomic load happens before the second load. That also implies the first write (W1) happens before the second (W2). However, there is no guarantee that W2 will be observed by another goroutine. I think what is really needed here is an atomic store byte operation. If this is the only goroutine writing to this buffer, you can emulate that by atomic.LoadUint32, set the highest/lowest byte, then atomic.StoreUint32 > > I did read through https://go.dev/ref/mem and > https://github.com/golang/go/discussions/47141 + links, but could not > find a definitive answer to my specific use-case. > > Would really appreciate any help getting to the bottom of this! > > -- > 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. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/CAMrvTSKXb5JQMR9PcCXwYhcT4rq8O_5hiTHrOChk6sUeOrbagw%40mail.gmail.com > <https://groups.google.com/d/msgid/golang-nuts/CAMrvTSKXb5JQMR9PcCXwYhcT4rq8O_5hiTHrOChk6sUeOrbagw%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > -- 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. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAMV2RqpAFDV9DJcydk3DD8%3DkdizJygBirq6Ub-VkKV2xK120Lw%40mail.gmail.com.