[翻译]  Should a lock variable be declared volatile?

[CHINESE]  锁定变量应该声明为volatile吗?


I have the following Lock statement:

我有以下Lock声明:

private readonly object ownerLock_ = new object();

lock (ownerLock_)
{
}

Should I use volatile keyword for my lock variable?

我应该为我的锁变量使用volatile关键字吗?

private readonly volatile object ownerLock_ = new object();

On MSDN I saw that it usually used for a field that is accessed without locking, so if I use Lock I don't need to use volatile?

在MSDN上我看到它通常用于没有锁定访问的字段,所以如果我使用Lock我不需要使用volatile?

From MSDN:

来自MSDN:

The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock statement to serialize access.

volatile修饰符通常用于多个线程访问的字段,而不使用lock语句来序列化访问。

2 个解决方案

#1


19  

If you're only ever accessing the data that the lock "guards" while you own the lock, then yes - making those fields volatile is superfluous. You don't need to make the ownerLock_ variable volatile either. (You haven't currently shown any actual code within the lock statement, which makes it hard to talk about in concrete terms - but I'm assuming you'd actually be reading/modifying some data within the lock statement.)

如果您在拥有锁时只访问锁定“守卫”的数据,那么是 - 使这些字段变得多余是多余的。您也不需要将ownerLock_变量设置为volatile。 (你当前没有在lock语句中显示任何实际代码,这使得很难用具体的术语来讨论 - 但我假设你实际上是在读取/修改lock语句中的一些数据。)

volatile should be very rarely used in application code. If you want lock-free access to a single variable, Interlocked is almost always simpler to reason about. If you want lock-free access beyond that, I would almost always start locking. (Or try to use immutable data structures to start with.)

volatile应该很少用在应用程序代码中。如果您想要对单个变量进行无锁访问,则Interlocked几乎总是更容易理解。如果你想在此之外进行无锁访问,我几乎总是会开始锁定。 (或者尝试使用不可变数据结构开始。)

I'd only expect to see volatile within code which is trying to build higher level abstractions for threading - so within the TPL codebase, for example. It's really a tool for experts who really understand the .NET memory model thoroughly... of whom there are very few, IMO.

我只希望在代码中看到volatile,它试图为线程构建更高级别的抽象 - 例如,在TPL代码库中。对于真正理解.NET内存模型的专家来说,它真的是一个工具......对于那些人很少,IMO。

#2


2  

If something is readonly it's thread-safe, period. (Well, almost. An expert might be able to figure out how to get a NullReferenceException on your lock statement, but it wouldn't be easy.) With readonly you don't need volatile, Interlocked, or locking. It's the ideal keyword for multi-threading, and you should use it where ever you can. It works great for a lock object where its big disadvantage (you can't change the value) doesn't matter.

如果某些东西是readonly它是线程安全的,期间。 (好吧,差不多。专家可能能够弄清楚如何在你的lock语句中获得NullReferenceException,但这并不容易。)使用readonly你不需要volatile,Interlocked或lock。它是多线程的理想关键字,您应该尽可能地使用它。它对锁定对象很有用,它的最大缺点(你不能改变它)无关紧要。

Also, while the reference is immutable, the object referenced may not be. "new object()" is here, but if it was a List or something else mutable--and not thread-safe--you would want to lock the reference (and all other references to it, if any) to keep the object from changing in two threads at once.

此外,虽然引用是不可变的,但引用的对象可能不是。 “new object()”在这里,但如果它是一个List或其他可变的东西 - 而不是线程安全的 - 你会想要锁定引用(以及对它的所有其他引用,如果有的话)来保持对象从一次改变两个线程。


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2018 ITdaan.com 粤ICP备14056181号