Why doesn't Thread implement IDisposable?
Asked Answered
S

3

10

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?

Selmner answered 11/11, 2009 at 17:45 Comment(0)
P
8

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.

Pronoun answered 11/11, 2009 at 17:49 Comment(4)
Since ManagedThreadID is an integer, and can be read even before a thread is started, that would suggest that something is allocated when a thread is created (even if just the number itself). While it would be odd for an application to create 2+billion Threads objects, it would seem the ids would be a resource that might be worthy of release.Relict
@supercat: Interesting point. ManagedThreadId appears to be allocated sequentially - I don't know what would happen after you've created over 2 billion of them.Pronoun
The ids seem to get reused when Thread objects become eligible for finalization, so I suspect Threading.Thread is implementing Finalize but not IDIsposable.Relict
I'm not sure that that covers it. Thread is, in many cases, like Process, in that it is just a handle to an native resource - ie. an operating system thread - Process implements IDisposable, so why not Thread? Both have finalizers, for example.Sclera
M
3

That's probably because you can't dispose of a thread. Instead you can ask it to die using Abort() or alike.

Mounts answered 11/11, 2009 at 17:50 Comment(1)
You can but in general the use of Abort is not considered good practice - just ensure the thread method returns (using a bool or similar) and the system clears up the rest.Darb
S
1

This is kind of a design question, so anyone who was not involved in building this aspect of .NET can only speculate. That being said, this blog post makes a good point:

...implementing IDisposable would not make any difference, at least in the current implementation of Thread. I increased the number of threads being created and the handle count goes down at some point so there is some mechanism for closing them

Threads do naturally clean up after themselves, so they are not a resource that needs to be managed in the typical sense.

Shooter answered 11/11, 2009 at 17:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.