On Sun, Oct 30, 2022 at 10:16 AM Konstantin Khomoutov <kos...@bswap.ru> wrote:
>
> | initially, y==0.
> |
> | goroutine 1:
> | x = 42
> | x = 99
> | atomic.Store(&y, 1)
> | x = 100
> |
> | goroutine 2:
> | if atomic.Load(&y) == 1 {
> |    println(x)
> | }
>
> then would println have been guaranteed to still print 99 and not 100?

No.  (But it definitely wouldn't print 42.)

> In other words, do I understand correctly that an atomic write-like operation
> guarantees that all stores made by a goroutine before that operation must be
> visible, but stores made afterwards may be visible or not visible?

Yes.

> Or, to put it differently, if the read from y in goroutine 2 sees a 1,
> there's no way println(x) prints 42, but it may print 99 or 100.

Yes.

> ..and also if x is not naturally aligned (I think it's impossible in Go) or
> not "hardware atomic" - like a 64-bit integer on a 32-bit platform, -
> println(x) could produce some weird value as a result of the torn write/torn
> read which may happen when one core updates the x while another one reads it,
> right?

Well, this case is not supported in general, but I think the answer is yes.

Ian

-- 
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/CAOyqgcUrQ1SeEaeX6dmnUCcQXp1cSyQw2nYiiXb2LiP1-C_qHQ%40mail.gmail.com.

Reply via email to