什么是锁(本)和锁(thisLock)之间的区别? [英] What is the difference between lock(this) and lock(thisLock)?

查看:303
本文介绍了什么是锁(本)和锁(thisLock)之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在读 lock语句(C#参考)我在哪里看到这个代码:

I'm reading lock Statement (C# Reference) where I saw this code:

class Account
{
    private Object thisLock = new Object();

    //...

    int Withdraw(int amount)
    {
        lock (thisLock)
        {
           //....
        }
    }

    //...
}

我不知道是否会作出任何区别,如果我们写锁(本)而不是在上面的例子中锁定(thisLock)。请参阅如果你的答案取决于它的完整的例子的。

I'm wondering if it would make any difference if we write lock(this) instead of lock(thisLock) in the above example. Please see the complete example if your answer depends on it.

如果您认为确实是有锁(本)锁之间的一些差异( thisLock),那么请帮我了解了所有重要的点差。特别是,什么是每个究竟的意思吗?

If you think there is indeed some difference between lock(this) and lock(thisLock) , then please help me understanding the difference with all important points. In particular, what does each exactly mean?

推荐答案

的差异从事实,即锁定,如果使用不当,可能会导致线程死锁。如果锁定目标的知名度是您不知道的(也就是说,你不能100%确定谁拥有对目标的引用,如果/当搞不好锁定它),那么你可真的不知道,如果应用程序可能会遇到死锁。

The difference stems from the fact that lock, if misused, can result in thread deadlock. If the lock target's visibility is unknown to you (i.e., you cannot be 100% certain who has a reference to the target and if/when they might lock it), then you cannot really know if the application might experience a deadlock.

有关这个原因,它是习惯性地锁定私人成员:因为它是私有的,它是在你的代码,你的的知道,没有人别的能锁定

For this reason it's customary to lock on a private member: since it's private and it's in your code, you do know that noone else can lock it.

当然,这一切的是一个纯粹的学术差大部分时间(一般人不会到处锁定随机对象),但它是一个很好的防守编码实践。

Of course all this is a purely academic difference most of the time (usually people don't go around locking random objects), but it's a good defensive coding practice.

你链接到状态页面:

在一般情况下,避免锁定在一个公开的类型,或超过$ b $实例b码的控制。常见的构造锁(本),锁(typeof运算
(的MyType))和锁(myLock)违反此准则:

In general, avoid locking on a public type, or instances beyond your code's control. The common constructs lock (this), lock (typeof (MyType)), and lock ("myLock") violate this guideline:

锁(本)是,如果实例可以被公开访问的问题。

lock (this) is a problem if the instance can be accessed publicly.

由于其他人可能使用他们有一个参考锁定实例,你的代码,做锁(本)当然不会指望。 上IDEone 示例 (见第26行)。

Because someone else might lock the instance using a reference they have, and your code that does lock(this) will of course not expect that. Example on IDEone (see line 26).

锁(typeof运算(的MyType))是一个问题,如果MyType的是公开访问。

lock (typeof (MyType)) is a problem if MyType is publicly accessible.

的变体上述情况,如果该类型是其他代码可见,那么你可能最终会试图锁定同一个实例作为代码(的typeof 返回单实例)。

A variation of the above, if the type is visible to other code then you might end up trying to lock the same instance as that code (typeof returns singleton instances).

锁(myLock)是一个问题,因为使用相同的字符串的过程中
任何其他码,将共享相同的锁。

lock("myLock") is a problem because any other code in the process using the same string, will share the same lock.

另一个变化:由于字符串实习,代码最终试图锁定同一个实例

Another variation: due to string interning, code ends up trying to lock the same instance.

最佳做法是定义一个私有对象锁定,或者私人
静态对象变量,以保护共同所有实例的数据。

Best practice is to define a private object to lock on, or a private static object variable to protect data common to all instances.

这篇关于什么是锁(本)和锁(thisLock)之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆