Search code examples
c++debuggingatomicvisual-studio-debugging

Atomic: if statement is entered even though the conditional is false


Edit: minimal reproducible example

#include <atomic>
int main()
{
    std::atomic_uint atomic_write_position{ 0 };
    unsigned write_position = atomic_write_position.fetch_add(1);
    bool b = false;
    b = atomic_write_position.compare_exchange_weak(write_position, 0);
    if (b);
    {
        auto x = 0;
    }
}

In this example, compare_exchange_weak should fail because atomic_write_position=1, write_position=0, and does behave correctly returning false and over writing write_position with the value of atomic_write_position. However the if statement after does not behave correctly, and is entered even though b is false.

I'm using visual studio 16.9.2


Solution

  • Remove the semi-colon ; in the if-statement