Consider this code:
#include <iostream>
using namespace std;
int main()
{
bool lock = false;
lock = __sync_val_compare_and_swap( &lock, false, true );
cout << lock << endl;
}
I expect the result to be displayed as 1 but the o/p is 0. Just calling __sync_val_compare_and_swap( &lock, false, true ); (so the return value is not captured) and then displaying lock results in 1 being displayed.
What am I missing here?
From the GCC doco:
Seems to me that 0 is the right value. I think you are incorrectly assigning "…the contents of
*ptrbefore the operation" tolock.This should output sensible results: