IDisposable and Thread Safety

added by rcash
11/3/2011 9:15:21 AM

3 Kicks, 60 Views

IDisposable is a standard interface in the .NET framework. This article discusses the importance of using it in parallel or multithread programming.


3 comments

dpeterson
11/3/2011 9:16:08 AM
Locking is the correct solution here as we're talking about stuff far more advanced than something like a simple getter/setter. It's imperative that the disposable stuff only gets called from one thread at a time.

vijayst
11/3/2011 12:27:04 PM
I am always curious to know something about the IDispose interface. How does the compiler know that we have to implement a protected virtual Dispose(bool) method? Or make the class sealed?

Noldorin
11/3/2011 10:35:01 PM
This is a good point to highlight, but I do not believe it is the most efficient.

I personally use an Interlocked-based method to good effect here. There is really no need for something as "heavy" as locking here.