c++ volatile

volatile关键字用于标记变量可能被意外修改,防止编译器优化。文章详细介绍了C/C++标准对volatile的定义,讨论了编译器优化可能导致的指令重排问题,包括编译器指令重排、CPU指令重排和x86指令重排。总结中指出volatile适用于内存映射和信号处理,并强调其在单核x86系统中能防止指令重排,但在多核场景下仍需注意。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

含义

  这个关键字的含义就是代表这个变量是不可预期地可能会突然值就会被改变,编译器不要对它做任何假设

c/c++标准对volatile的描述
  1. C 语言:Every access (both read and write) made through an lvalue expression of volatile-qualified type is considered an observable side effect for the purpose of optimization and is evaluated strictly according to the rules of the abstract machine (that is, all writes are completed at some time before the next sequence point). This means that within a single thread of execution, a volatile access cannot be optimized out or reordered relative to another visible side effect that is separated by a sequence point from the volatile access.
  2. C++ 语言:Every access (read or write operation, member function call, etc.) made through a glvalue expression of volatile-qualified type is treated as a visible side-effect for the purposes of optimization (that is, within a single thread of execution, volatile accesses cannot be optimized out or reord
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值