Is there any sense to make atomic objects with the qualifier - volatile?
使用限定符- volatile的原子对象有什么意义吗?
Use that:
使用:
volatile std::atomic<int> i(1);
instead of:
而不是:
std::atomic<int> i(1);
2 个解决方案
#1
9
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
).
不,是完全没有意义的std::原子也不稳定,在std::原子,代码将处理变量的可能性随时可能改变,和其他处理器可能需要“告诉”,它改变了(“告诉”其他处理器不是由不稳定)。
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.
唯一一次你真正需要的是如果你有一个指针波动硬件代码是控制——例如阅读一个计数器在一个计时器,或一个帧缓冲现在很活跃,或讲述一个网卡在哪里读下一个数据包发送的数据。这些东西是不稳定的,因为编译器不知道这些东西的值在任何时候都可以改变。
#2
4
Usually, it doesn't make sense.
通常,这是没有意义的。
Use atomic
to allow the variable to be modified on one thread while other threads may be accessing it without explicit synchronisation.
使用atomic(原子)允许在一个线程上修改变量,而其他线程可以在没有显式同步的情况下访问它。
Use volatile
to control access to unusual memory locations (such as hardware registers), where every read and write must happen in the order specified by the program. A normal variable, atomic or otherwise, doesn't usually require such control.
使用volatile控制对不常见内存位置(如硬件寄存器)的访问,在这些位置,每次读写必须按照程序指定的顺序进行。一个普通的变量,原子的或者其他的,通常不需要这样的控制。
The two concepts are unrelated to each other. In particular, do not confuse volatile
with a keyword used in other languages to make variables atomic. In C++, volatile
has nothing to do with thread interactions whatsoever.
这两个概念互不相关。特别是,不要将volatile与其他语言中用于使变量具有原子性的关键字混淆。在c++中,volatile与线程交互无关。
#1
9
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
).
不,是完全没有意义的std::原子也不稳定,在std::原子,代码将处理变量的可能性随时可能改变,和其他处理器可能需要“告诉”,它改变了(“告诉”其他处理器不是由不稳定)。
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.
唯一一次你真正需要的是如果你有一个指针波动硬件代码是控制——例如阅读一个计数器在一个计时器,或一个帧缓冲现在很活跃,或讲述一个网卡在哪里读下一个数据包发送的数据。这些东西是不稳定的,因为编译器不知道这些东西的值在任何时候都可以改变。
#2
4
Usually, it doesn't make sense.
通常,这是没有意义的。
Use atomic
to allow the variable to be modified on one thread while other threads may be accessing it without explicit synchronisation.
使用atomic(原子)允许在一个线程上修改变量,而其他线程可以在没有显式同步的情况下访问它。
Use volatile
to control access to unusual memory locations (such as hardware registers), where every read and write must happen in the order specified by the program. A normal variable, atomic or otherwise, doesn't usually require such control.
使用volatile控制对不常见内存位置(如硬件寄存器)的访问,在这些位置,每次读写必须按照程序指定的顺序进行。一个普通的变量,原子的或者其他的,通常不需要这样的控制。
The two concepts are unrelated to each other. In particular, do not confuse volatile
with a keyword used in other languages to make variables atomic. In C++, volatile
has nothing to do with thread interactions whatsoever.
这两个概念互不相关。特别是,不要将volatile与其他语言中用于使变量具有原子性的关键字混淆。在c++中,volatile与线程交互无关。