什么时候需要 const volatile 对象? [英] When are const volatile objects necessary?

查看:58
本文介绍了什么时候需要 const volatile 对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意: 我确实理解 pointersconst volatile 内存位置的需要,但这些不需要对象本身constvolatile.
我问的是某些 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:

  1. 在 setjmp 范围内标记局部变量,以便该变量在 longjmp 后不会回滚.
  2. 内存被外部代理修改或似乎是由于错误的内存映射
  3. 信号处理程序恶作剧

所以基本上你只想将其他特性用于并发编程,并为那些罕见的情况保存 volatile

So basically you want to really only use other features for concurrent programming and save volatile for those rare situations

这篇关于什么时候需要 const volatile 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆