.net 处理方法的线程安全性?

niknxzdl  于 2023-07-01  发布在  .NET
关注(0)|答案(4)|浏览(176)

MSDN很好地记录了BCL类型的示例成员的线程安全性,但我从未真正看到过如何调用IDisposable类型的Dispose方法的信息。
Dispose方法是否a)保证对所有类都是线程安全的,b)从不保证是线程安全的,c)保证对某些类是线程安全的(如果是,在哪里有专门的文档说明)?
最后,如果Dispose方法被保证是线程安全的,这是否意味着我必须在类中使用一次性资源的每个示例方法周围放置一个锁?
附带说明:我知道类型的终结器应该是线程安全的,因为垃圾收集在.NET中的工作方式(相当激进),它们可能会调用Dispose方法。但是,让我们把这个问题放在这里。

holgip5t

holgip5t1#

线程安全和Dispose的问题有些棘手。由于在许多情况下,一旦任何其他线程开始释放对象,任何线程可以合法地对对象执行的唯一事情就是尝试自己Dispose,因此乍一看,确保线程安全的唯一必要的事情似乎是在'dispose'标志上使用Interlocked.Exchange,以确保一个线程的Dispose尝试发生,而另一个线程被默默忽略。事实上,这是一个很好的起点,我认为它应该是标准Dispose模式的一部分(CompareExchange应该在密封的基类 Package 器方法中完成,以避免每个派生类都需要使用自己的私有dispose标志)。不幸的是,如果考虑Dispose实际上做了什么,事情就复杂得多了。
Dispose的真实的目的不是对被释放的对象做什么,而是清理该对象持有引用的其他实体。这些实体可以是被管理对象、系统对象或完全其他的东西;它们甚至可能不在与被处理的对象相同的计算机上。为了使Dispose是线程安全的,那些其他实体将允许Dispose在其他线程可能对它们做其他事情的同时清理它们。有些对象可以处理这种用法;其他人不能。
一个特别恼人的例子:允许对象具有带有非线程安全的RemoveHandler方法的事件。因此,任何清理事件处理程序的Dispose方法都只能从创建订阅的线程所在的线程调用。

r7s23pms

r7s23pms2#

MSDN上的页面实际上从未明确指出Dispose方法不是线程安全的,但根据我的阅读,它们的代码暗示不,它们不是线程安全的,如果需要,您需要考虑这一点。
特别是示例代码中的注解:

  1. // This class shows how to use a disposable resource.
  2. // The resource is first initialized and passed to
  3. // the constructor, but it could also be
  4. // initialized in the constructor.
  5. // The lifetime of the resource does not
  6. // exceed the lifetime of this instance.
  7. // This type does not need a finalizer because it does not
  8. // directly create a native resource like a file handle
  9. // or memory in the unmanaged heap.
  10. public class DisposableResource : IDisposable
  11. {
  12. private Stream _resource;
  13. private bool _disposed;
  14. // The stream passed to the constructor
  15. // must be readable and not null.
  16. public DisposableResource(Stream stream)
  17. {
  18. if (stream == null)
  19. throw new ArgumentNullException("Stream in null.");
  20. if (!stream.CanRead)
  21. throw new ArgumentException("Stream must be readable.");
  22. _resource = stream;
  23. _disposed = false;
  24. }
  25. // Demonstrates using the resource.
  26. // It must not be already disposed.
  27. public void DoSomethingWithResource() {
  28. if (_disposed)
  29. throw new ObjectDisposedException("Resource was disposed.");
  30. // Show the number of bytes.
  31. int numBytes = (int) _resource.Length;
  32. Console.WriteLine("Number of bytes: {0}", numBytes.ToString());
  33. }
  34. public void Dispose()
  35. {
  36. Dispose(true);
  37. // Use SupressFinalize in case a subclass
  38. // of this type implements a finalizer.
  39. GC.SuppressFinalize(this);
  40. }
  41. protected virtual void Dispose(bool disposing)
  42. {
  43. // If you need thread safety, use a lock around these
  44. // operations, as well as in your methods that use the resource.
  45. if (!_disposed)
  46. {
  47. if (disposing) {
  48. if (_resource != null)
  49. _resource.Dispose();
  50. Console.WriteLine("Object disposed.");
  51. }
  52. // Indicate that the instance has been disposed.
  53. _resource = null;
  54. _disposed = true;
  55. }
  56. }
  57. }
展开查看全部
gzszwxb4

gzszwxb43#

我相当肯定,除非另有说明,否则任何类的Dispose()方法都将被视为“示例成员”,以便在文档中指示线程安全与否。
因此,如果文档声明示例成员不是线程安全的,那么Dispose()也不一定是线程安全的,除非特别指出它与其他成员不同。

cig3rfwq

cig3rfwq4#

Dispose只是一个方法,没有什么特别的。它的特殊用例是using语句-所以你不必记住写
try { do something with object} finally { object.Dispose();}
所以Dispose不是线程安全的--为什么它应该是唯一调用它的线程应该是拥有它的线程。
永远不要从多个线程中对单个对象调用Dispose。虽然Dispose可能不是线程安全的,但如果你有多个线程在处理同一个对象,那么你就有了麻烦。
不要在多个线程上释放单个对象。这不仅是一个反模式(谁拥有对象?)而是设计上的错误。
如果您使用Interlocked.Exchange来同步释放,那么您就丢失了对象所有者是谁。

相关问题