In C++ code, but not specific to C++ and could just as easily be expressed in many languages:
atomic<int> counter(0);
// Many threads
counter.fetch_add(1); // lock xadd
// One thread
std::lock_guard<std::mutex> lock(mutex);
counter.exchange(INT_MIN); // lock xchg
Is it possible to have an execution where the xadd reads a value of counter from before the store of INT_MIN, then the store of INT_MIN happens, and the incremented value of counter clobbers INT_MIN - thus it's possible that the counter never goes negative (well assuming it doesn't wrap around, which would be undefined anyway.)
I want to know if that store with xchg can get "lost". If so I really need to use CAS instead of fetch_add. However, I suspect since lock xadd specifically has to protect against losing updates that its execution cannot be interleaved with locked instructions on other threads (at the very least with other lock xadd!)