Quantcast
Channel: How does std::atomic ensure atomicity - Stack Overflow
Viewing all articles
Browse latest Browse all 3

How does std::atomic ensure atomicity

$
0
0

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?


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images