volatile in C++

1) "volatile" has nothing to do with multithreading. It is not atomic.
2) The compiler will not reorder the sequence of "volatile" variables, but may reorder the sequence of "volatile" and non-volatile. CPU may reorder the execution of "volatile" variables (CPU memory order, depending on the CPU vendor).

3) In a single threaded env, "volatile int vi = 0", vi = 1; int read_back = vi; assert (read_back == 1); the assertion may fail. WHY ? (think of "vi" is mapping to an hardware port. Memory mapped I/O).
4) The compiler will not do any optimization for volatile variables. It generates code which always read the value from the memory.
5) "volatile" doesn't guarantee Acquire-Release semantics. "Acquire": read an atomic, the code after the "read of an atomic" will NOT be reordered before the read. "Release" : write an atomic, the code before the "write of an atomic" will NOT be reordered after the write (everything will be committed to memory, and seen by other CPUs).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值