The following code:
#include <atomic>
std::atomic_bool b;
void store2() {
b.store(true, std::memory_order::relaxed);
b.store(true, std::memory_order::relaxed);
}
GCC, MSVC, and clang all emit two mov
s like this:
store2():
mov BYTE PTR b[rip], 1
mov BYTE PTR b[rip], 1
ret
b:
.zero 1
See live demo
Would it be allowed to coalesce these atomic stores into a single mov
? I don't believe it would violate the restrictions of std::memory_order::relaxed
if other threads observed both atomic stores simultaneously, as if it was just one.
If it is allowed, why don't compilers perform this simple optimization?