线程安全的处置方法? [英] Thread-Safety of Dispose methods?

查看:147
本文介绍了线程安全的处置方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MSDN文档中的BCL pretty的实例成员的线程安全好,但我从来没有真正见过指示信息是如何的处置方法的IDisposable 类型都可以调用。

MSDN documents the thread-safety of instances members of BCL types pretty well, but I have never really seen information indicating how the Dispose method of IDisposable types can be called.

处置方法的)保证是线程安全的所有类,B)永远保证是线程安全的,C)保证是线程安全的对于一些类(如果是的话,这哪里是专门文件)?

Is the Dispose method a) guaranteed to be thread-safe for all classes, b) never guaranteed to be thread-safe, c) guaranteed to be thread-safe for some classes (if so, where is this specifically documented)?

最后,如果处置方法是保证线程安全的,这意味着我必须把锁周围的每个实例方法在使用一次性资源类?

Finally, if the Dispose method is guaranteed to be thread-safe, does that mean I have to put a lock around each instance method in the class that uses disposable resources?

侧一点:我知道,终结的类型应该是线程安全的,由于垃圾收集工作于.NET(非常积极)的方式,他们可能会潜在地调用处置方法。然而,让我们离开这个问题留给这里的要点。

Side point: I'm aware that finalizers for types ought to be thread-safe due to the way garbage collection works in .NET (quite aggressively), and they may potentially call the Dispose method. However, let's leave this issue aside for the point here.

推荐答案

线程安全和处置的问题是有点棘手。因为在许多情况下,唯一的事情,任何线程可以合法地与对象,一旦任何其他线程做的已经开始出售它擅自处理它本身,它会乍一看似乎是既要保证线程安全将是唯一用一个'处置'标志Interlocked.Exchange,以确保一个线程的处置企图发生,另一种是忽略。事实上,这是一个很好的起点,我认为它应该是标准的Dispose模式(该CompareExchange应该在密封的基类的包装方法已经完成,以避免需要为每一个派生类中使用自己的私人的一部分设置标志)。不幸的是,如果考虑什么处置实际上做,事情要复杂得多。

The issue of thread-safety and Dispose is somewhat tricky. Since in many cases the only thing that any thread may legitimately do with an object once any other thread has started to dispose it is attempt to Dispose it itself, it would at first blush seem like the only thing necessary to ensure thread safety would be to use Interlocked.Exchange on a 'disposed' flag to ensure that one thread's Dispose attempt happens and the other is silently ignored. Indeed, that's a good starting point, and I think it should have been part of the standard Dispose pattern (the CompareExchange should have been done in the sealed base-class wrapper method, to avoid the need for every derived class to use its own private disposed flag). Unfortunately, if one considers what Dispose actually does, things are much more complicated.

处置的真正目的不是为了做一些物体被布置,而是收拾到该对象持有引用其他实体。这些实体可以被管理的对象,系统对象,或者其他什么东西完全;它们甚至可能不是在同一计算机上作为对象被布置。对于处置是线程安全的,与其他实体将允许处置清除它们在同一时间,其他线程可能会做其他的事情他们。一些对象可以处理这种用法;别人做不到的。

The real purpose of Dispose is not to do something to the object being disposed, but rather to clean up other entities to which that object holds references. These entities may be managed objects, system objects, or something else entirely; they may not even be on the same computer as the object being disposed. For Dispose to be thread-safe, those other entities would to allow Dispose to clean them up at the same time as other threads might be doing other things with them. Some objects can handle such usage; others cannot.

一个特别棘手的例子:对象可以与那些不是线程安全的RemoveHandler方法事件。因此,该清理的事件处理程序的任何Dispose方法只应调用在同一个线程在其中创建订阅的人。

One particular vexing example: Objects are allowed to have events with RemoveHandler methods that are not thread-safe. Consequently, any Dispose method which cleans up event handlers should only be called from the same thread as the one in which the subscriptions were created.

这篇关于线程安全的处置方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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