Back in the good old days of 68k, both pre/post pointer increments/decrements were more or less guaranteed to be atomic, due to the relatively general relationship between the stack pointer and other more generic address registers. As this behaviour is clearly a 68k artefact which may not procure the most efficient implementation on an alternate platform, would I be correct开发者_运维问答 in assuming that the C specification makes no claims as to the "atomicity" of these operations.
would I be correct in assuming that the C specification makes no claims as to the "atomicity" of these operations.
Yes. The current standard (both C99 and C89, IIRC) makes no claims about atomicity. That is definitely platform-specific, and probably depends on the compiler as well.
Why is it necessary for these operations to be atomic, if I may ask?
C standard have no atomic operations at all. However there are some GCC built-ins described here, and there are some in C++0X stanard. And you always can use POSIX or other multithreading library or inline assembler in your code.
Regardless of what the standard specifies, 80x86 pointer arithmetic and assignments on "huge" pointers was not atomic, but pointer increments and assignments are apt to be composed of an atomic read and an atomic write on all platforms where "int" is 32 bits or larger or where a pointer is the same size as an 'int' (some embedded platforms have things like 16 bit int but 24-bit pointer). The read-modify-write sequence is almost never going to form an atomic unit on any multi-processor architecture, however, unless one takes explicit measures to make it behave as such.
There is never a guarantee of atomicity unless explicitly documented. Under the current C standard, that means that no portable code can guarantee atomicity -- atomic operators are necessarily platform-specific.
As @ninjalj notes in his comment, C1x will add atomic operators.
精彩评论