Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does GCC use mov/mfence instead of xchg to implement C11's atomic_store?

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?

like image 266
tibbe Avatar asked Jul 23 '14 12:07

tibbe


1 Answers

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.

like image 106
Vality Avatar answered Oct 24 '22 07:10

Vality