10

I'm trying to use __atomic_load_n from the gcc atomic builtins page, compiling with

gcc -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 the arch and the march flags (and made sure by setting the std=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 vanilla gcc 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);
}
8
  • Can you show us the code that's giving you the error?
    – NPE
    Commented Dec 18, 2012 at 21:00
  • @NPE: sure, the questions has been updated. Commented Dec 18, 2012 at 21:04
  • 1
    I can compile your code no problem (gcc 4.7.2) once I typedef type to int. 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?
    – NPE
    Commented Dec 18, 2012 at 21:05
  • @NPE: Oh, well. Looks like it's time to recompile gcc :( There goes an hour. Thanks. Commented Dec 18, 2012 at 21:06
  • 1
    I believe the __atomic_* functions were added in 4.7. Previous versions have __sync_* functions which fulfill a similar purpose.
    – Kerrek SB
    Commented Dec 18, 2012 at 21:13

1 Answer 1

19

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.

2
  • While __ATOMIC_SEQ_CST is always valid, it may give you pretty suboptimal results, of couirse depending on the memory model of your CPU. Commented Dec 8, 2015 at 14:18
  • Thanks, well answered! Commented May 6, 2017 at 8:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.