May 15

Interlocked exchange on Linux: undefined reference to `__sync_add_and_fetch_4′

I am pretty conservative in selecting the tools I use for my projects, thus sticking with good old C++. To be really productive on C++ - using of some sort of smart pointers is a must. And to make things a bit more complicated there are multi-core machines, that run different OS'es and the project has to build correctly for 32 and 64 bits. Here we got things to protect...Using atomic barriers in GCC (supported since v.4.1.0 or so) may allow significant performance gain in comparison to pthread' mutexes. Though I've not done any benchmarks of this matter on Linux (yet), the gain on Windows while migrating from critical sections to interlocked exchange was pretty significant.The GCC built-in functions for atomic memory access work wonders: http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Atomic-Builtins.htmlThe only problem - it did not compile on 32 bit Linux, throwing the following linker error at me:

undefined reference to `__sync_add_and_fetch_4'

Defining the compiler's option:

-march=pentium

solved the issue (i486 worked here Ok too).