When are const volatile objects necessary?(什么时候需要 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
这篇关于什么时候需要 const volatile 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:什么时候需要 const volatile 对象?
基础教程推荐
- 初始化列表*参数*评估顺序 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- CString 到 char* 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
