可能重复:
Can a C# thread really cache a value and ignore changes to that value on other threads?使用横纱布尔中止线程
比方说我们有这样的代码:
bool KeepGoing = true;
DataInThread = new Thread(new ThreadStart(DataInThreadMethod));
DataInThread.Start();
//bla bla time goes on
KeepGoing = false;
private void DataInThreadMethod()
{
while (KeepGoing)
{
//Do stuff
}
}
}
现在的想法是,使用布尔是一种终止线程的安全方法,但是因为调用线程上存在该布尔值会导致任何问题?
那布尔仅用于调用线程停止线程所以它不喜欢它的其他地方正在使用
KeepGoing只是程序中的成员/字段。 – Jon 2010-03-25 09:32:09
使用volatile的x64系统有问题吗?我读过这个“使用x64架构,这不是最优化的”。那是什么意思? – Jon 2010-03-25 09:37:31
@Jon - 很难回答这种情况,但我认为*这意味着它可能不会被缓存在寄存器中。但国际海事组织,你应该去代码,总是*工作。 – 2010-03-25 11:55:52