Monday July 25th

Saturday July 23rd

Friday July 22nd

3 Kicks

IDisposable and Thread Safety

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


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.

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?

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.

Commenting on Stories is limited for now and will open up to those recommended by the community. Learn how
Loading DotNetKicks...
brought to you by the Kicks Network