If I have a code a = a + 1
, now I understand that there are multiple CPU level operations required to execute this, but how does definining a
as std::atomic<int>
make these multiple transactions atomic?
Does it change the way CPU instruction are executed. I'd assume that it will have to shrink the number of instructions to 1 somehow, so that any context switching does not cause unreliable results, but how does it do that?
If the compiler can always create code like that, why not always do that?