C++核心准则CP.200:使用volatile只能表明该变量是非C++内存
CP.200: Use volatile only to talk to non-C++ memory
CP.200:使用volatile只能表明该变量是非C++内存
Reason(原因)
volatile is used to refer to objects that are shared with "non-C++" code or hardware that does not follow the C++ memory model.
volatile用于表明参照的对象需要和非C++代码或硬件共享而遵守C++内存模型。
Example(示例)
const volatile long clock;
This describes a register constantly updated by a clock circuit. clock is volatile because its value will change without any action from the C++ program that uses it. For example, reading clock twice will often yield two different values, so the optimizer had better not optimize away the second read in this code:
这段代码描述一个不断被时钟电路更新的寄存器。clock被定义为volatile是因为它的值在使用它的C++程序没有任何动作的情况下被修改。例如,两次读取clock经常可以得到不同的值,因此优化器最好不要优化掉这段代码中的第二个读操作。
long t1 = clock;
// ... no use of clock here ...
long t2 = clock;
clock is const because the program should not try to write to clock.
clock定义为常量是为了表明程序不应该对clock进行写操作。
Note(注意)
Unless you are writing the lowest level code manipulating hardware directly, consider volatile an esoteric feature that is best avoided.
除非你正在编写直接操作硬件的低层次代码,否则将volatile作为冷门功能并最好避免使用。
Example(示例)
Usually C++ code receives volatile memory that is owned elsewhere (hardware or another language):
通常情况下,C++代码接受有其他某处拥有的volatile内存(硬件或其他语言):
int volatile* vi = get_hardware_memory_location();
// note: we get a pointer to someone else's memory here
// volatile says "treat this with extra respect"
Sometimes C++ code allocates the volatile memory and shares it with "elsewhere" (hardware or another language) by deliberately escaping a pointer:
某些C++代码会分配volatile内存并通过故意泄露指针的方式和其他部分共享(硬件或其他语言)它。
static volatile long vl;
please_use_this(&vl); // escape a reference to this to "elsewhere" (not C++)
Example, bad(反面示例)
volatile local variables are nearly always wrong -- how can they be shared with other languages or hardware if they're ephemeral? The same applies almost as strongly to member variables, for the same reason.
volatile类型的局部变量几乎一定是错的--如果它们只能短期存在,怎么能分享给其他语言或硬件呢?由于同样的原因,该原则也几乎一定适用于成员变量。
void f()
{
volatile int i = 0; // bad, volatile local variable
// etc.
}
class My_type {
volatile int i = 0; // suspicious, volatile member variable
// etc.
};
Note(注意)
In C++, unlike in some other languages, volatile has nothing to do with synchronization.
和其他语言不同,在C++中不会为同步做任何事情。
Enforcement(实施建议)
- Flag volatile T local and member variables; almost certainly you intended to use atomic<T> instead.
- 标记volatile类型的局部变量和成员变量;几乎可以肯定的说你想用的其实是atomatic<T>。
- ???
原文链接https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#cp200-use-volatile-only-to-talk-to-non-c-memory
- Elasticsearch索引增量统计及定时邮件实现
- 机器学习中的特征空间
- 简单易学的机器学习算法——马尔可夫链蒙特卡罗方法MCMC
- 推荐算法——基于图的推荐算法PersonalRank算法
- 推荐算法——非负矩阵分解(NMF)
- 【Go 语言社区】转-golang windows 判断锁屏
- 【Go 语言社区】单点redis 持久化在高并发下存在延迟情况
- 设计模式(2)-策略模式之多用组合少用继承
- Golang获取随机端口和本机ip地址
- 设计模式(3)-装扮你的类(装饰模式)
- [Go 语言社区]服务器读取配置文件只-json数据
- gsoap开发webservice
- [Go 语言社区]测试模块之---utf8例子
- org.hibernate.type.StringType cannot be cast to org.hibernate.type.VersionType
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 面试中的路由问题
- BOSS直聘招聘信息获取之使用webdriver进行爬取
- es6中class的一些基础知识和es5语法的对比
- Windows 系统常用命令行命令(一):前言
- Vue项目使用mt-picker实现省市区三级联动踩坑记录
- Windows 系统常用命令行命令(二):路径与文件夹操作
- 微信小程序订阅消息推送
- 2020年中央一号文件说了啥?
- mpvue框架下小程序分享朋友圈功能实现
- Stata与Python等效操作与调用
- 杂乱文件自动分类
- ABAP Development Tool前后台交互的原理
- Stata | covid19 命令下载疫情数据
- Stata&Python | 分别实现多元线性回归
- Stata | 用 frames 来“分蛋糕”