ali_bahoo
ali_bahoo

Reputation: 4873

Compiler and CPU reordering

I have this 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 either CPU or the compiler reorders the lines of SetValues() function?

Upvotes: 3

Views: 1218

Answers (1)

Ben Voigt
Ben Voigt

Reputation: 283893

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

As far as CPU reordering is concerned, the MSDN documentation says "This function generates a full memory barrier (or fence) to ensure that memory operations are completed in order."

Upvotes: 3

Related Questions