本文介绍了有什么意义,使std ::原子<>具有限定符的对象 - volatile?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有任何意义使原子对象与限定词 - volatile?

Is there any sense to make atomic objects with the qualifier - volatile?

使用:

volatile std::atomic<int> i(1);

而不是:

std::atomic<int> i(1);


推荐答案

不, c $ c> std :: atomic 也是volatile,因为在 std :: atomic 中,代码将处理变量的可能性改变,并且其他处理器可能需要被告知它已经改变(告诉其他处理器不被 volatile 覆盖)。

No, there is absolutely no sense in making std::atomic also volatile, as inside the std::atomic, the code will deal with the possibility that the variable may change at any time, and that other processors may need to be "told" that it has changed (the "telling" other processors is not covered by volatile).

你真正需要 volatile 的唯一方法是如果你有一个指向你的代码控制硬件的指针 - 例如读取定时器中的计数器,或者哪个帧缓冲器现在是活动的,或者告诉网卡在哪里读取要发送的下一个分组的数据。这些东西是不稳定的,因为编译器不能知道这些东西的价值可以随时改变。

The only time you really need volatile is if you have a pointer to piece of hardware that your code is controlling - for example reading a counter in a timer, or a which frame buffer is active right now, or telling a network card where to read the data for the next packet to send. Those sort of things are volatile, because the compiler can't know that the value of those things can change at any time.

这篇关于有什么意义,使std ::原子&lt;&gt;具有限定符的对象 - volatile?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 04:05