Compiler and processor reordering

I have the following situation.

volatile double val1 = 10.0;
volatile double val2 = 20.0;

double SetValues(double d1, double d2)
{
  double ret = d1-d2;
  InterlockedExchange64( (volatile long long*)&val1, *((long long*)&d1) ); // val1 = d1;
  InterlockedExchange64( (volatile long long*)&val2, *((long long*)&d2) ); // val2 = d2;
  return val1 - va2;
}

My question is: is it possible that the processor or compiler reorders the function strings SetValues()?

+3
source share
1 answer

The compiler can never reorder function calls in external libraries. If your compiler implements these functions as built-in, it will be smart enough not to reorder them.

Regarding processor reordering, the MSDN documentation says: "This function generates a complete memory barrier (or fence) to ensure that memory operations are performed in order."

+3
source

All Articles