Click here to Skip to main content
13,193,970 members (48,845 online)
Rate this:
Please Sign up or sign in to vote.
See more:

I have managed C++ code in which i am using Thread.
I have started Thread like below

ThreadStart* threadStart = new ThreadStart(pThread, CThread::MyFile);
Thread* oThread = new Thread(threadstart);

Once my requested job done. I have to stop thread. How can i do this?
Is it really required to stop the thread? I was assuming that once my requested job will done, Thread will automatically stop itself.

Any suggestion?

Posted 25-Sep-12 7:59am
Sergey Alexandrovich Kryukov 25-Sep-12 14:30pm
One of the way is Thread.Abort, but read very thoroughly about it to understand how exactly it works -- this is not trivial and takes really delicate reasoning to use it safely. I use it, but I know what I do; and many consider this matter as very contradictory (but most don't know how it's really works). I don't want to cause a flame war here.
Another way is cooperative: the thread should exit its main method to get finished; and the other threads can only signal the suggestion to terminate...

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Please see my comments to the question. As you say "once my requested job is done", you don't need to do anything.
The thread will exit the method threadstart and it will stop it.

As to the object oThread, it will be destroyed by GC eventually, after the object becomes unreacheable.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.171018.2 | Last Updated 25 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100