为什么"锁(typeof运算(的MyType))"一个问题? [英] Why is "lock (typeof (MyType))" a problem?

查看:183
本文介绍了为什么"锁(typeof运算(的MyType))"一个问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MSDN给出了C#以下警告有关关键字:




在一般情况下,避免锁定在公共
型或实例超出你的代码中的
控制。常见的结构锁定
(本),锁(typeof运算(的MyType)),和
锁(myLock)违反此
方针:

  *锁(这)是一个问题,如果实例可以被公开访问。 
*锁(typeof运算(的MyType))是一个问题,如果MyType的是公开访问。




然而,它给出了它没有坚实的理由。锁(这)是在SO 这里解释。我感兴趣的锁(typeof运算(的MyType))的情况。什么是危险呢?



感谢您。


解决方案

这是危险的,因为这样很难或不可能防止死锁的情况什么都可以采取锁。



有曾经是这个一篇文章(不锁类型的对象! 一个GUI博士的文章),在与波多黎各马里亚尼一些意见。显然该制品是不再直接使用,但也有镜子左右浮动,包括在 http://bytes.com/topic/c-sharp/answers/249277-dont-lock-type-objects



下面是摘录




基本的这里的问题是,你没有自己的类型的对象,而且你不知道还有谁可以访问它。一般情况下,这是一个非常糟糕的主意依靠锁定你没有创建,不知道还有谁可能被访问的对象。这样做邀请僵局。 。最安全的方法是只锁定私有对象



但等待;它比一切更糟糕。事实证明,类型对象有时是跨应用程序域(但不跨进程)在.NET运行时的当前版本的共享。 (因为他们是不可变这通常是好的。)这意味着它可能在不同的应用领域,甚至运行其他应用程序(但在同一个进程),以一个类型对象上获得一个锁要锁定死锁您的应用程序从不释放它。这将是很容易得到访问该类型的对象,因为该对象有一个名字,该类型的全名!请记住,锁/为SyncLock块(这对挂起一个礼貌的字),直到它可以获取锁。这显然​​真的很糟糕依靠锁定另一个程序或组件可以锁定,使你死锁。



MSDN gives the following warning about the lock keyword in C#:

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.
* lock (typeof (MyType)) is a problem if MyType is publicly accessible.

Yet it gives no solid reasoning for it. The lock(this) is explained here on SO. I'm interested in lock(typeof(MyType)) case. What is dangerous about it?

Thank you.

解决方案

It's dangerous because anything can take that lock so it's difficult or impossible to prevent a deadlock situation.

There used to be an article on this ("Don't Lock Type Objects!" a Dr. GUI article) in with some comments by Rico Mariani. Apparently the article is no longer directly available, but there are 'mirrors' floating around, including at http://bytes.com/topic/c-sharp/answers/249277-dont-lock-type-objects.

Here's an excerpt:

The basic problem here is that you don't own the type object, and you don't know who else could access it. In general, it's a very bad idea to rely on locking an object you didn't create and don't know who else might be accessing. Doing so invites deadlock. The safest way is to only lock private objects.

But wait; it's even worse than all that. As it turns out, type objects are sometimes shared across application domains (but not across processes) in current versions of the .NET runtime. (This is generally okay since they're immutable.) That means that it's possible for ANOTHER APPLICATION running even in a different application domain (but in the same process) to deadlock your application by getting a lock on a type object you want to lock and never releasing it. And it would be easy to get access to that type object because the object has a name—the fully qualified name of the type! Remember that lock/SyncLock blocks (that's a polite word for hangs) until it can obtain a lock. It's obviously really quite bad to rely on a lock that another program or component can lock and cause you to deadlock.

这篇关于为什么"锁(typeof运算(的MyType))"一个问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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