Using gcc atomic builtins?

19,022

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.

Share:
19,022
Dervin Thunk
Author by

Dervin Thunk

Updated on June 04, 2022

Comments

  • Dervin Thunk
    Dervin Thunk almost 2 years

    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);
    }
    
    • NPE
      NPE over 11 years
      Can you show us the code that's giving you the error?
    • Dervin Thunk
      Dervin Thunk over 11 years
      @NPE: sure, the questions has been updated.
    • NPE
      NPE over 11 years
      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?
    • Dervin Thunk
      Dervin Thunk over 11 years
      @NPE: Oh, well. Looks like it's time to recompile gcc :( There goes an hour. Thanks.
    • Kerrek SB
      Kerrek SB over 11 years
      I believe the __atomic_* functions were added in 4.7. Previous versions have __sync_* functions which fulfill a similar purpose.
    • Yann Droneaud
      Yann Droneaud over 11 years
      as @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
      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
      Kerrek SB over 11 years
      @DervinThunk: OK, done :-)
  • class stacker
    class stacker over 8 years
    While __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
    Howard Shane about 7 years
    Thanks, well answered!