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 对象?
基础教程推荐
- 我有静态或动态 boost 库吗? 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 常量变量在标题中不起作用 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 这个宏可以转换成函数吗? 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
