Reputation: 33029
I'm using GCC's atomic builtins to increment a shared volatile int
. All I want to do is increment the value, and I don't care about the return value. Should I prefer fetch_and_add
or add_and_fetch
for this? I don't see anything in the documentation suggesting one would have a performance benefit over the other. Does it really not matter?
Alternatively, since I don't actually need to read the value atomically, is there a better way to increment a volatile int
since I don't actually read the return value?
This is obviously architecture dependent, so you can assume I'm using x64. I'm specifically using AMD Opteron CPUs if that makes a difference.
Upvotes: 1
Views: 910
Reputation: 736
It depends on your hardware, depend on the size of your variable, on the increment value and on your compiler options.
To check it, translate a little C program in to assembly.
volatile int init;
void foo(void) {
__sync_fetch_and_add(&init,1);
__sync_add_and_fetch(&init,1);
}
Translates with 'gcc -S x.c' into a file x.s. The most interesting in x.s is
lock addl $1, init(%rip)
lock addl $1, init(%rip)
That shows the two intrinsics are translated into the same assembler codes (here on Intel x64).
You could also try it with -march=native and/or -O3 or your favourite options just to see if it is always the same...
Upvotes: 2