为什么语法需要对象

为什么语法需要对象

本文介绍了C#“锁定"关键字:为什么语法需要对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要将代码标记为关键部分,我们可以这样做:

  Object lockThis = new Object();锁(lockThis){//关键部分} 

为什么必须有一个对象作为锁定语法的一部分?换句话说,为什么这不能相同:

 锁{//关键部分} 
解决方案

因为您不只是锁定-您锁定了某物(您锁定了一个锁)./p>

锁定的要点是不允许两个线程直接竞争同一资源.因此,您将该资源隐藏在任意对象后面.该任意对象充当锁.当一个线程进入关键部分时,它将锁定该锁,而其他线程则无法进入.当该线程在关键部分中完成其工作时,它将解锁并且将密钥留给下一个碰到的线程.

如果一个程序有一个可以竞争访问的资源,那么它也可能会拥有其他这样的资源!但是这些资源通常彼此独立-换句话说,一个线程能够锁定一个特定资源,而另一个线程同时锁定另一个资源却不受这两个资源的干扰可能是有意义的.>

还可能需要从两个关键部分访问资源.这两个将需要具有相同的锁.如果每个人都有自己的资源,那么它们将无法有效地保持资源的无争议性.

那么,显然,我们不只是锁定-我们锁定每个特定资源自己的锁定.但是编译器无法静默地自动生成该任意锁定对象,因为它不知道应该使用相同的锁定来锁定哪些资源,而哪些资源应该具有自己的锁定.这就是为什么您必须明确声明哪个锁可以保护哪个代码块(或 blocks ).

请注意,正确使用对象作为锁要求对象是持久的(至少与相应资源一样持久).换句话说,您不能在本地作用域中创建它,也不能将其存储在本地变量中,并且在作用域退出时将其丢弃,因为这实际上并没有锁定任何内容.如果您有一个持久对象充当给定资源的锁,则只有一个线程可以进入该部分.如果您每次有人尝试进入时都创建一个新的锁定对象,那么任何人都可以随时进入.

To mark code as a critical section we do this:

Object lockThis = new Object();
lock (lockThis)
{
     //Critical Section
}

Why is it necessary to have an object as a part of the lock syntax? In other words, why can't this work the same:

lock
{
   //Critical Section
}
解决方案

Because you don't just lock - you lock something (you lock a lock).

The point of locking is to disallow two threads from directly competing for the same resource. Therefore, you hide that resource behind an arbitrary object. That arbitrary object acts as a lock. When one thread enters a critical section, it locks the lock and the others can't get in. When the thread finishes its work in the critical section, it unlocks and leaves the keys out for whichever thread happens to come next.

If a program has one resource that's candidate for competing accesses, it's possible that it will have other such resources as well! But often these resources are independent from each other - in other words, it may make sense for one thread to be able to lock one particular resource and another thread in the meantime to be able to lock another resource, without those two interfering.

A resource may also need to be accessed from two critical sections. Those two will need to have the same lock. If each had their own, they wouldn't be effective in keeping the resource uncontested.

Obviously, then, we don't just lock - we lock each particular resource's own lock. But the compiler can't autogenerate that arbitrary lock object silently, because it doesn't know which resources should be locked using the same lock and which ones should have their own lock. That's why you have to explicitly state which lock protects which block (or blocks) of code.

Note that the correct usage of an object as a lock requires that the object is persistent (at least as persistent as the corresponding resource). In other words, you can't create it in a local scope, store it in a local variable and throw it away when the scope exits, because this means you're not actually locking anything. If you have one persistent object acting as a lock for a given resource, only one thread can enter that section. If you create a new lock object every time someone attempts to get in, then anyone can enter at all times.

这篇关于C#“锁定"关键字:为什么语法需要对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 07:59