如果它们在线程间共享,将变量标记为volatile很有用吗? [英] Is it useful to mark variables as volatile if they are shared across threads?
问题描述
注意!
我显然没有在这里向大家说明这一点,令人难以置信的沮丧。我的目标是消除神话, volatile
实际上是一个无操作,它什么也不做。
I'm obviously failing to make my point clearly to everyone here, and it's incredibly frustrating. My goal was to dispel the myth that volatile
is effectively a no-op, that it does nothing. I was not trying to state that it should be used, that it is essential, that it is not redundant, etc.
我已经证明, volatile
仍然做一件事。我承认在某些情况下它是多余的,并且多线程示例是一个糟糕的选择。
I have shown that volatile
does still do a thing. I concede that it is redundant under certain circumstances, and that the multi-threaded example was a poor choice.
我也不想隐藏的事实,我的答案的初始修订包含错误。但这个Q& A甚至没有接近实现其预期目的。为此,我认为现在是时候吸取它。
I am also not trying to hide the fact that my answer's initial revisions contained errors. But this Q&A is not even coming close to fulfilling its intended purpose. To that end, I think it's time to chuck it away.
感谢Kerrek和T.C.为他们的见解。我只是不认为他们的回答适合我想问的问题。我相信这是我错误的问。
Thanks to Kerrek and T.C. for their insights. I just don't think their responses fit the question that I wanted to ask. I'm sure that's my fault for asking it poorly.
因此,我放弃了它!关闭它是一个重复的问题,不是它的意图,但它被解释为。
Therefore I am abandoning it! And closing it as a duplicate of the question not that it was intended as, but that it has been interpreted as.
干杯! (& hth。)
Cheers! (& hth.)
我正在写一个线程中的变量,并从另一个线程中读取。我已经被告知, volatile
是完全无用的这一点,我不需要使用它在这一天和年龄,除非我使用硬件。
I am writing to a variable in one thread and reading from it in another. I have been told that volatile
is completely useless for this and that I do not need to use it in this day and age unless I am working with hardware.
int x = 0;
void thread1()
{
while (true) {
sleep(1);
if (x > 0)
break;
}
}
void thread2()
{
while (true) {
sleep(1);
x++;
}
}
我通过使用 volatile
在这种情况下?
如果 x
不是一个简单的 int
但是类类型?
Do I gain anything by using volatile
in this case?
And how about if x
is not a simple int
but a class type?
推荐答案
/ code>对于线程间通信不是有用的。不要将其用于此目的。它不提供同步,并让您的代码与数据竞争。相反,当正确同步对共享状态的访问时,您不需要 volatile
。
You have been told correctly, volatile
is not useful for inter-thread communication. Don't use it for that purpose. It does not provide synchronization and leaves your code with data races. Conversely, when synchronizing access to shared state correctly, you do not need volatile
.
您的意思使用原子变量共享状态或保护共享状态与互斥,共享状态将被所有线程正确观察。例如:
The correct code that Does What You Mean uses atomic variables for the shared state or protects the shared state with a mutex, and the shared state will be observed correctly by all threads. For example:
#include <atomic>
std::atomic<int> x = 0;
void thread1()
{
while (true) {
sleep(1);
if (x > 0)
break;
}
}
void thread2()
{
while (true) {
sleep(1);
x++;
}
}
在任何时候都不需要 volatile
。
请注意, volatile
一个线程强制执行一个没有副作用的循环:
Note that volatile
may be useful within a thread to enforce that a loop that otherwise has no side effects is evaluated:
// Spend some time
for (volatile int i = 0; i != LARGE_NUMBER; ++i)
{ /* do nothing */ }
// or even:
for (int i = 0; const_cast<volatile int &>(i) != LARGE_NUMBER; ++i) {}
这篇关于如果它们在线程间共享,将变量标记为volatile很有用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!