编译器和CPU重新排序
我有以下这种情况。
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;
}
我的问题是,CPU 或编译器是否有可能重新排序 SetValues() 函数的行?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编译器永远无法重新排序对外部库中函数的调用。如果您的编译器将这些函数实现为内在函数,那么它就会足够聪明,不会对它们重新排序。
就CPU重新排序而言,MSDN文档说“这个函数生成完整的内存屏障(或栅栏)以确保内存操作按顺序完成。”
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."