Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
Hi,
 
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);
oThread->Start();
 
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?
 
Regards,
AJ83
Posted 25-Sep-12 7:59am
AJ831.2K
Comments
Sergey Alexandrovich Kryukov at 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...
--SA

1 solution

Rate this: bad
good
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.
 
—SA
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 375
1 Prakriti Goyal 242
2 Yogesh Kumar Tyagi 224
3 Sergey Alexandrovich Kryukov 158
4 CPallini 150
0 OriginalGriff 6,742
1 Sergey Alexandrovich Kryukov 5,479
2 Maciej Los 3,474
3 Peter Leow 3,313
4 DamithSL 2,505


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 25 Sep 2012
Copyright © CodeProject, 1999-2014
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