什么时候需要 const volatile 对象?
注意: 我确实理解 pointers 到 const volatile
内存位置的需要,但这些不需要对象本身const
或 volatile
.
我问的是某些 const volatile
类型的 它们自己 的对象,例如:
Note: I do understand the need for pointers to const volatile
memory locations, but those don't require the objects themselves to be const
or volatile
.
I'm asking about objects that are themselves of some const volatile
type, for example:
const volatile T obj;
这些在哪些情况下是必要的或有用的?
In which situations are these necessary or useful?
推荐答案
在 c++ 中真正需要 volatile
的情况很少见.volatile
不再对多线程有用.来自 这个网站 volatile 的使用只有三种可移植.
The situations are rare where when you actually need volatile
in c++. volatile
is not useful for multithreaded any more. From this website there are only three portable uses of volatile.
Hans Boehm 指出 volatile 只有三种便携式用途.我将在这里总结它们:
Hans Boehm points out that there are only three portable uses for volatile. I'll summarize them here:
- 在 setjmp 范围内标记局部变量,以便该变量在 longjmp 后不会回滚.
- 内存被外部代理修改或似乎是由于错误的内存映射
- 信号处理程序恶作剧
所以基本上你只想将其他特性用于并发编程,并为那些罕见的情况保存 volatile
So basically you want to really only use other features for concurrent programming and save volatile for those rare situations
相关文章