Using gcc atomic builtins?
Up until GCC 4.6.3, compiler built-ins for atomic operations were a pure compiler extension, and in GCC they were grouped into the __sync_*
family of functions.
As of version 4.7.0, both the new C++11 and the C11 standards had been finalized, and GCC updated their atomic built-ins to better reflect the new memory model of those two new language revisions. The new functions are grouped into the __atomic_*
family.
However, the older built-ins are still available, and the documentation says this:
It is always safe to replace a
__sync
call with an__atomic
call using the__ATOMIC_SEQ_CST
memory model.
Dervin Thunk
Updated on June 04, 2022Comments
-
Dervin Thunk almost 2 years
I'm trying to use
__atomic_load_n
from thegcc
atomic builtins page, compiling withgcc -Wall -march=i686 -std=gnu99 ll.c -o ll
but it tells me it can't
warning: implicit declaration of function ‘__atomic_load_n’
I thought it would be enough to provide
gcc
with thearch
and themarch
flags (and made sure by setting thestd=gnu99
flag), but to no avail. In fact, even if I test for the common__GCC_VERSION__
or__GNUC__
macros don't seem to have values... but I have a pretty vanillagcc
installation, the one that comes in Ubuntu.I know I'm doing something silly, but I can't figure out what. I have gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Code looks like this: it's a function that never gets called (yet), so the problem is at compile time.
type* func(type* p) { type* q = __atomic_load_n (p, __ATOMIC_SEQ_CST); }
-
NPE over 11 yearsCan you show us the code that's giving you the error?
-
Dervin Thunk over 11 years@NPE: sure, the questions has been updated.
-
NPE over 11 yearsI can compile your code no problem (gcc 4.7.2) once I typedef
type
toint
. If you suspect a problem with your compiler installation, perhaps try to build a non-trival but clean project with it to see what happens? -
Dervin Thunk over 11 years@NPE: Oh, well. Looks like it's time to recompile gcc :( There goes an hour. Thanks.
-
Kerrek SB over 11 yearsI believe the
__atomic_*
functions were added in 4.7. Previous versions have__sync_*
functions which fulfill a similar purpose. -
Yann Droneaud over 11 yearsas @KerrekSB said, this was introduced in GCC 4.7 Check GCC 4.6.3 documentation: gcc.gnu.org/onlinedocs/gcc-4.6.3/gcc
-
Dervin Thunk over 11 years@KerrekSB: Since you were the first to say this, mind adding yours as an answer so I can accept it?
-
Kerrek SB over 11 years@DervinThunk: OK, done :-)
-
-
class stacker over 8 yearsWhile
__ATOMIC_SEQ_CST
is always valid, it may give you pretty suboptimal results, of couirse depending on the memory model of your CPU. -
Howard Shane about 7 yearsThanks, well answered!