为什么摆脱挥发性危险? [英] Why is it dangerous to get rid of volatile?
问题描述
在C ++中, volatile
的处理方式与 const
相同:将易失性数据的指针传递给函数不希望 volatile
修饰符触发编译错误。
In C++, volatile
is treated the same way const
is: passing a pointer to volatile data to a function that doesn't want the volatile
modifier triggers a compile error.
int foo(int* bar) { /* snip */ }
int main()
{
volatile int* baz;
foo(baz); // error: invalid conversion from ‘volatile int*’ to ‘int*’
}
为什么它是危险的?对于 const
修饰符很明显,删除它可以破坏 const
正确性;但是有这样的事情 volatile
正确性吗?我不知道如何将指针传递到易失性数据作为指向非易失性数据的指针可能会导致问题。
Why is it dangerous? It's obvious for the const
modifier that removing it can break const
correctness; but is there such a thing as "volatile
correctness"? I can't figure out how passing a pointer to volatile data as a pointer to non-volatile data could cause problems.
编辑就是这样你们知道为什么我第一次使用 volatile
:许多Mac OS X的 OSAtomic
系列函数原子增量,减量,加法,减法,比较和交换等)接受 volatile
参数
EDIT Just so you guys know why I was using volatile
in the first place: many of Mac OS X's OSAtomic
family of functions (for atomic increments, decrements, additions, subtractions, compare and swap, etc.) takes volatile
arguments.
推荐答案
编译器不仅可以优化对非易失性变量的访问,而且只要程序的顺序执行不受影响,就可以进行预测性或推测性更新。
Not only can the compiler optimize away access to non-volatile variables, it can update them predictively/speculatively, as long as the sequential execution of the program is unaffected.
如果虚假写入您的volatile变量不会破坏您的设计,它可能不需要在任何上下文中是volatile。
If spurious writes to your volatile variable don't break your design, it probably didn't need to be volatile in any context.
例如,C ++ 03编译器转换完全合法。
For example, it is perfectly legal for the C++03 compiler to transform
int result;
void sum_if_all_positive( std::array<N> ary )
{
int sum = 0;
result = -1;
for( int i = 0; i < N; ++i ) {
if (ary[i] < 0) return;
sum += ary[i];
}
result = sum;
}
到
int result;
void sum_if_all_positive( std::array<N> ary )
{
result = 0;
for( int i = 0; i < N; ++i ) {
if (ary[i] < 0) { result = -1; return; }
result += ary[i];
}
}
(虽然这样的改变会提供比注册更好的性能总共只有几个架构,具有便宜的存储器访问和很少的寄存器。Microchip PIC架构是可以想到的。)
(Although such a change would provide better performance than enregistering sum only on a few architectures with cheap memory access and very few registers. The Microchip PIC architecture comes to mind.)
这篇关于为什么摆脱挥发性危险?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!