Should I make sure the threads end at Dispose?

Asked

Viewed 175 times

4

There are already some questions about the interface IDisposable and the using, for example:

What types of resources are released in a statement using

I should always use Disposis

Still I’m not sure what to do about threads.

In my mind if the object uses any thread explicitly, it makes sense to ensure that this thread end the use of thread.join() for example.

This would serve to ensure there is no work to be done in background but would also decrease the resources (in this case CPU) used by this threads.

In addition I also ensured that the threads would be in a known (terminated) state, rather than in an unknown state.

I know the CPU is not a resource that can be reserved by thread and that there is nothing that guarantees that my thread is using it.

But my premise is this: If I can assure you that there is no work to be done on threads, why not do it?

What should I do anyway? I must ensure that mine threads end or not?

2 answers

7


Bruno, whenever your class has members who implement IDisposable you MUST do the correct Disposis of these members in your class.

Thread does not implement IDisposable, so you didn’t MUST, but you certainly CAN close the thread, for the reasons you commented.

But I suggest not using a Thread object directly but using resources more modern as Threadpool or, better yet, Task (TPL).

  • I get it. It’s the typical duality of must vs can found in documents ISO. Maybe it’s a problem that must be solved in a method other than Dysplasia

3

Minelli’s answer already says the basics, I decided to answer because it originated in a question from which I answered and had my comments.

If the class Thread does not implement IDisposable you can’t call the Dispose() no matter how much you want to.

It may be a problem of terminology, but finishing a thread has nothing to do with discarding your resources (Handle of the operating system for example). The Dispose() is to rule out, not to finish.

In the question What types of resources are released in a "using statement"? that originated this talks about the use of using, therefore there is no possibility of its use in the class Thread, even if you want to. You can do something to prevent thread stay alive if you want, but not with the using, will not be calling Dispose(). No doubt you need to take some care of it to ensure its completion, but not with using. You gotta make sure she gets stopped, if you make sure of that, it’s all right.

But create threads It is very expensive, it is not appropriate to keep creating or killing them. You need to create and reuse what you have. For this we use tasks whenever possible. Or we create a mechanism that simulates more or less what Task makes. A task may or may not manipulate threads, She herself doesn’t do it, she may just need something asynchronous.

The fact of a thread continuing to exist does not mean that she is consuming some resource, she may be in stationary state without consuming any CPU, it will only consume memory for your stack and some small information of her administration.

The class is already used Task, there is different, this class can, and normally should use the using or a pattern that does more or less the same thing as the using makes. Then was created the ValueTask who does not need, and she is preferred whenever possible.

  • I have come to understand better what the dilemma is with your answer. And then I come to the conclusion that the work of finishing a thread should be done in another method. As I suggested in my comment to Mineli’s reply. I do not know which answer to accept, I preferred to write the user with less reputation, since his answer is also good.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.