jeudi 4 août 2016

Can std::atomic cancel out increments with decrements?

Using relaxed memory order, e.g. for a reference counting pointer, would the compiler be allowed to optimize away a 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 disassembly it doesn't look like. But since reordering is allowed and the atomic is behaving like a counter, just thread-safe, one could argue that he could optimize as if it would be a plain int.

Aucun commentaire:

Enregistrer un commentaire