C# 中的 Volatile 和 Thread.MemoryBarrier
为了实现多线程应用程序的无锁代码,我使用了易失性
变量, 理论上:易失性
关键字只是用来确保所有线程都能看到易失性变量的最新值;因此,如果线程 A
更新变量值,并且线程 B
在更新发生后立即读取该变量,它将看到最近从线程 A 写入的最新值。 正如我在C# 4.0 in a Nutshell一书中读到的那样 这是不正确因为
应用易失性并不会阻止先写后读的交换。
是否可以通过在每次获取 volatile 变量之前放置 Thread.MemoryBarrier() 来解决此问题,例如:
private volatile bool _foo = false;
private void A()
{
//…
Thread.MemoryBarrier();
if (_foo)
{
//do somthing
}
}
private void B()
{
//…
_foo = true;
//…
}
如果这解决了问题;考虑我们有一个 while 循环,它依赖于其条件之一的该值;将 Thread.MemoryBarrier() 放在 while 循环之前是解决问题的正确方法吗?例如:
private void A()
{
Thread.MemoryBarrier();
while (_someOtherConditions && _foo)
{
// do somthing.
}
}
为了更准确,我希望 _foo 变量在任何线程随时请求时提供最新的值;因此,如果在调用变量之前插入 Thread.MemoryBarrier()
可以解决问题,那么我可以使用 Foo
属性而不是 _foo
并执行 Thread.MemoryBarrier()
在该属性的获取中,例如:
Foo
{
get
{
Thread.MemoryBarrier();
return _foo;
}
set
{
_foo = value;
}
}
To implement a lock free code for multithreading application I used volatile
variables,
Theoretically: The volatile
keyword is simply used to make sure that all threads see the most updated value of a volatile variable; so if thread A
updates the variable value and thread B
read that variable just after that update is happened it will see the most updated value that written recently from thread A.
As I read in a C# 4.0 in a Nutshell book that
this is incorrect because
applying volatile doesn’t prevent a write followed by a read from being swapped.
Could this problem being solved by putting Thread.MemoryBarrier()
before every get of the volatile
variable like:
private volatile bool _foo = false;
private void A()
{
//…
Thread.MemoryBarrier();
if (_foo)
{
//do somthing
}
}
private void B()
{
//…
_foo = true;
//…
}
And if this solves the problem; consider we have a while loop that depend on that value at one of its conditions; is putting Thread.MemoryBarrier()
before the while loop is a correct way to fix the issue? example:
private void A()
{
Thread.MemoryBarrier();
while (_someOtherConditions && _foo)
{
// do somthing.
}
}
To be more accurate I want the _foo
variable to give its most fresh value when any thread asking for it at any time; so if inserting Thread.MemoryBarrier()
before calling the variable fixes the issue then could I use Foo
property instead of _foo
and do a Thread.MemoryBarrier()
within the get of that property Like:
Foo
{
get
{
Thread.MemoryBarrier();
return _foo;
}
set
{
_foo = value;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
“C# In a Nutshell”是正确的,但它的说法没有实际意义。为什么?
让我们澄清一下。拿你的原始代码来说:
如果线程调度程序已经检查了
_foo
变量,但它在//do some
注释之前被挂起,会发生什么?好吧,此时您的其他线程可能会更改_foo
的值,这意味着您所有的 volatile 和 Thread.MemoryBarriers 都毫无意义!如果_foo
的值为 false 时绝对必须避免do_something
,那么您别无选择,只能使用锁。但是,如果当
_foo
突然变为 false 时,可以执行do some
,那么这意味着 volatile 关键字足以满足您的需求。需要明确的是:所有告诉您使用记忆屏障的响应者都是不正确的或提供了过度杀伤力。
The "C# In a Nutshell" is correct, but its statement is moot. Why?
Let's clarify. Take your original code:
What happens if the thread scheduler has already checked the
_foo
variable, but it gets suspended just before the//do something
comment? Well, at that point your other thread could change the value of_foo
, which means that all your volatiles and Thread.MemoryBarriers counted for nothing!!! If it is absolutely essential that thedo_something
be avoided if the value of_foo
is false, then you have no choice but to use a lock.However, if it is ok for the
do something
to be executing when suddenly_foo
becomes false, then it means the volatile keyword was more than enough for your needs.To be clear: all the responders who are telling you to use a memory barrier are incorrect or are providing overkill.
这本书正确。
CLR 的内存模型表明加载和存储操作可以重新排序。这适用于易失性和非易失性变量。
将变量声明为
易失性
仅意味着加载操作将具有获取语义,而存储操作将具有释放语义。此外,编译器将避免执行某些优化,这些优化依赖于以序列化、单线程方式访问变量的事实(例如,将加载/存储提升到循环之外)。单独使用 volatile 关键字不会创建临界区,也不会导致线程神奇地相互同步。
当您编写无锁代码时,您应该极其小心。这并不简单,即使是专家也很难做到正确。
无论您试图解决的原始问题是什么,很可能有一种更合理的方法来解决它。
The book is correct.
The CLR's memory model indicates that load and store operations may be reordered. This goes for volatile and non-volatile variables.
Declaring a variable as
volatile
only means that load operations will have acquire semantics, and store operations will have release semantics. Also, the compiler will avoid performing certain optimizations that relay on the fact that the variable is accessed in a serialized, single-threaded fashion (e.g. hoisting load/stores out of loops).Using the
volatile
keyword alone doesn't create critical sections, and it doesn't cause threads to magically synchronize with each other.You should be extremely careful when you write lock free code. There's nothing simple about it, and even the experts have trouble to get it right.
Whatever is the original problem you're trying to solve, it's likely that there's a much more reasonable way to do it.
在第二个示例中,您还需要在循环内放置一个 Thread.MemoryBarrier(); ,以确保每次检查循环条件时都能获得最新值。
In your second example, you would need to also put a
Thread.MemoryBarrier();
inside the loop, to make sure you get the most recent value every time you check the loop condition.从此处提取...
因此,如果我们回到您的循环示例...它应该是这样的...
Pulled from here...
So if we go back to your looping example...this is how it should look...
微软自己关于内存屏障的说法是:
仅在具有弱内存排序的多处理器系统(例如,采用多个 Intel Itanium 处理器的系统)上才需要 MemoryBarrier。
对于大多数用途,C# lock 语句、Visual Basic SyncLock 语句或 Monitor 类提供了更简单的数据同步方法。
Microsoft's own words on memory barriers:
MemoryBarrier is required only on multiprocessor systems with weak memory ordering (for example, a system employing multiple Intel Itanium processors).
For most purposes, the C# lock statement, the Visual Basic SyncLock statement, or the Monitor class provide easier ways to synchronize data.