Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Thread implement IDisposable?

I noticed that System.Threading.Thread implements a finalizer but not IDisposable. The recommended practice is to always implement IDisposable when a finalizer is implemented. Jeffrey Richter wrote that the guideline is "very important and should always be followed without exception".

So why doesn't Thread implement IDisposable? It seem like implementing IDisposable would be a non-breaking change that would allow deterministic cleanup of Thread's finalizable resources.

And a related question: since thread is finalizable, do I have to hold references to running Threads to prevent them from being finalized during execution?

like image 838
Special Touch Avatar asked Nov 11 '09 17:11

Special Touch


1 Answers

What would disposing of a Thread object do? The "resource" in this case has its own natural clean-up - the thread finishing. Note that also the sense of ownership is missing... within the executing thread, you can always use Thread.CurrentThread, so only that thread would really able to claim any sort of ownership.

Basically I think Thread is a slightly unusual case - there's a lifetime to the underlying resource, but it isn't something that ought to be cleaned up explicitly.

like image 87
Jon Skeet Avatar answered Oct 15 '22 23:10

Jon Skeet