简体   繁体   English

为什么需要冗余锁定对象?

[英]Why is a redundant lock object needed?

Out of curiosity was looking at the lock keyword on MSDN : 出于好奇,我们在MSDN上查看了lock关键字:

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

    public void Withdraw(decimal amount)
    {
        lock (thisLock)
        {
            if (amount > balance)
            {
                throw new Exception("Insufficient funds");
            }
            balance -= amount;
        }
    }
}

In the example above the object thisLock is used with the lock keyword. 在上面的示例中,对象thisLocklock关键字一起使用。 Why is this needed? 为什么需要这个? It doesnt seem to have any other purpose. 它似乎没有任何其他目的。 Why not just have the lock keyword by itself? 为什么不单独拥有lock关键字?

lock keyword cannot exist on it's own, it always takes a parameter which will act as a semaphore (synchronizing object), allowing only one thread to proceed. lock关键字本身不存在,它总是需要一个参数作为信号量(同步对象),只允许一个线程继续。

http://www.albahari.com/threading/part2.aspx#_Locking http://www.albahari.com/threading/part2.aspx#_Locking

Only one thread can lock the synchronizing object (in this case, thisLock) at a time, and any contending threads are blocked until the lock is released. 一次只有一个线程可以锁定同步对象(在本例中为thisLock),并且任何竞争线程都会被阻塞,直到释放锁定为止。 If more than one thread contends the lock, they are queued on a “ready queue” and granted the lock on a first-come, first-served basis (a caveat is that nuances in the behavior of Windows and the CLR mean that the fairness of the queue can sometimes be violated). 如果多个线程争用锁定,它们会在“就绪队列”上排队,并按照先到先得的原则授予锁定(需要注意的是Windows和CLR行为的细微差别意味着公平性队列有时可能会被违反)。

There are several aspects here: 这里有几个方面:

  • The lock statements needs an object reference as identifier. lock语句需要一个对象引用作为标识符。 It has to have something that identifies this lock and separates it from any other locks that can exist in the code. 它必须具有标识此锁的东西,并将其与代码中可能存在的任何其他锁分开。

  • The data that you are protecting isn't a reference type, so you need something else to use as the identifier for the lock. 您要保护的数据不是引用类型,因此您需要使用其他内容作为锁的标识符。

  • It's recommended to have a private object that is only used as identifier for the lock, even if it would be possible to use the data iself as identifier. 建议使用仅用作锁的标识符的私有对象,即使可以使用数据iself作为标识符也是如此。 That way there is no reason to ever expose the reference outside the class, as that would open up for possible deadlocks if it was used in a lock outside the class. 这样就没有理由在类外部公开引用,因为如果它被用在类外的锁中,就会打开可能的死锁。

The object used for locking isn't redundant. 用于锁定的对象不是多余的。 The object acts as a token, which is used to implement a simple synchronization protocol: Whoever holds the lock is granted access to the locked code. 该对象充当令牌,用于实现简单的同步协议:持有锁的任何人都被授予对锁定代码的访问权限。 All others have to wait until the lock is released. 所有其他人都必须等到锁被释放。

Without an object it would not be possible to have different tokens and all synchronization would rely on a single internal token. 如果没有对象,则不可能有不同的令牌,并且所有同步都依赖于单个内部令牌。 That would not be very effective. 这不会很有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM