In C++ and Beyond 2012: Herb Sutter - atomic<> Weapons, 2 of 2 Herb Sutter argues (around 0:38:20) that one should use xchg
, not mov
/mfence
to implement atomic_store
on x86. He also seems to suggest that this particular instruction sequence is what everyone agreed one. However, GCC uses the latter. Why does GCC use this particular implementation?
Quite simply, the mov
and mfence
method is faster as it does not trigger a redundant memory read like the xchg
which will take time. The x86 CPU guarantees strict ordering of writes between threads anyway so so it is enough.
Note some very old CPUs have a bug in the mov
instruction which makes xchg
necessary but this is from a very long time ago and working around this is not worth the overhead to most users.
Credit to @amdn for the information on the bug in old Pentium CPUs causing xchg
to be needed in the past.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With