Using a relaxed memory order , for example. for a reference count pointer, will the compiler be allowed to optimize subsequent increment and decrement?
std::atomic_int32_t ai; for (size_t i = 0; i < 10000; i++) { ai.fetch_add(1, std::memory_order_relaxed); ai.fetch_sub(1, std::memory_order_relaxed); }
Looking at a showdown, it doesn't look like it. But since reordering is allowed and atomic behaves like a counter, just thread-safe, it can be argued that it could optimize as if it were a simple int.
c ++ compiler-optimization multithreading atomic c ++ 11
Borph
source share