An atomic method for std::atomic appears in the
C++11 standard. It is available as a proprietary extension to
C in the
Itanium ABI specification, and (with the same syntax) in
GCC.
x86 implementation In the x86 architecture, the instruction ADD with the destination operand specifying a memory location is a fetch-and-add instruction that has been there since the
8086 (it just wasn't called that then), and with the LOCK prefix, is atomic across multiple processors. However, it could not return the original value of the memory location (though it returned some flags) until the
486 introduced the XADD instruction. The following is a
C implementation for the
GCC compiler, for both 32- and 64-bit x86 Intel platforms, based on extended asm syntax: inline int fetchAndAdd(int* variable, int value) { asm volatile( "lock; xaddl %0, %1" : "+r" (value), "+m" (*variable) // input + output : // No input-only : "memory" ); return value; }
History Fetch-and-add was introduced by the
Ultracomputer project, which also produced a multiprocessor supporting fetch-and-add and containing custom VLSI switches that were able to combine concurrent memory references (including fetch-and-adds) to prevent them from serializing at the memory module containing the destination operand. ==See also==