问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/24909997/why-does-gcc-use-mov-mfence-instead-of-xchg-to-implement-c11s-atomic-store