On Fri, Sep 25, 2026 at 12:35 PM Andrey Tarasevich via Gcc <[email protected]> wrote: > I presume your answer is intended to state the same thing as > Martin's answer. However, I'm a bit perplexed by the code snippet > you supplied.
Based on my understanding, let me reword it (and yes, I'm intentionally using overly simplistic wording here)... Consider a CPU with four cores. Core 0 writes to X and core 1 writes to Y. Now cores 2 and 3 read from X and Y. Those changes have to propogate from cores 0 and 1 to cores 2 and 3, and cores 2 and 3 have to "see the same thing, at the right time." Marking those variables "atomic" tells gcc to use opcodes that meet that guarantee, in this case xchg has extra hardware to coordinate cross-core info about such things. Mov does not have that hardware, so it *might* happen correctly, but it's not *guaranteed*. Marking a variable "volatile" does not mean the same thing. "Atomic" means "must happen all at once, no matter who is looking" where "volatile" means "don't optimize away any reads or writes". GCC can read/write a volatile variable with two mov's if it needs to, but it will do those two mov's every time the program says to read/write it.
