On Tue, 2014-04-29 at 09:11 -0400, Leo Ferres wrote: > Hello, > > Suppose the following C code. > > int t = __sync_fetch_and_add(&s->r, 1); > > s is a struct with member r, which is an volatile int. > > Is this an atomic operation, even if you include the indirection?
The atomic operation is on a single memory location, so in this example only on the address you supply. > In > other words, can a thread come between the dereferencing operation and > the atomic increment of another thread? If so, is there a solution > besides whileing with a compare and swap? That depends on what the other threads are doing. If you need background on concurrent programming, I suggest consulting a textbook such as "The Art of Multiprocessor Programming" by Herlihy and Shavit. How to solve this is not specific to C11 atomics or GCC's built-ins. (Also, gcc-help@ would be a more appropriate list for this kind of question.)