My understanding is that atomic machine instructions may be up to two orders of magnitude slower than a non-atomic operation. For example, given
int x;
x++
and
std::atomic<int> y;
y++;
my understanding is that x++
typically runs much faster than y++
. (I'm assuming that the increment operation maps to an underlying machine instruction. I'm sure the exact comparative cost varies from architecture to architecture, but I'm talking about a rule of thumb.)
I'm interested in the relative cost of an atomic RMW operation and a non-inline function call, again, as a general rule of thumb. For example, given this non-inline function,
void f(){}
what can we generally say about the cost of y++
(i.e., the atomic increment) compared to the cost of executing a non-inline call to f
?
My motivation is to try to put the common claim that "atomic operations are much more expensive than non-atomic operations" in perspective. One way to do that is to try to get some idea how expensive an atomic RMW operation is compared to calling and returning from a non-inline function.
Please don't reply with "the only way to know is to measure." I'm not asking about an atomic RMW operation versus a function call in a particular context on a particular architecture. I'm asking about a general rule of thumb that could be used as the basis of discussion for people who might think, "We can never use atomic instructions, because they're too expensive," yet who wouldn't think twice about making function calls.