Click to See Complete Forum and Search --> : Working Thread Question
Dan|]
September 26th, 2005, 11:25 AM
Hi all,
Just a simple question for my own edification:
The only way to end a working thread is from the controlling function ?
If there is a way to stop it otherwise, please give me a hint.
Respectfully thanks,
DD
golanshahar
September 26th, 2005, 11:29 AM
How to end a thread? (http://www.codeguru.com/forum/showthread.php?t=305166)
Cheers
Siddhartha
September 26th, 2005, 11:30 AM
']The only way to end a working thread is from the controlling function ?
If there is a way to stop it otherwise, please give me a hint.What exactly do you mean by "Controlling Function"?
The best way to end a thread is simply to return from the "Thread Function".
There exist force tactics like TerminateThread (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/terminatethread.asp) to end a thread forcibly from outside it.
The usage of this API is not recommended.
Siddhartha
September 26th, 2005, 11:31 AM
[ redirected ]
Regards,
Siddhartha
Dan|]
September 26th, 2005, 11:37 AM
Accordind to MSDN:
CWinThread* AfxBeginThread(
AFX_THREADPROC pfnThreadProc,
LPVOID pParam,
int nPriority = THREAD_PRIORITY_NORMAL,
UINT nStackSize = 0,
DWORD dwCreateFlags = 0,
LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL
);
"pfnThreadProc
Points to the controlling function for the worker thread"
My question was the thread can be ended only from this pfnThreadProc.
Siddhartha
September 26th, 2005, 11:40 AM
']My question was the thread can be ended only from this pfnThreadProc.The answer is that the best way for the thread to end is by returning from that function.
As you can see in my previous post - there are other ways as well.
Ways that are not recommended.
Dan|]
September 26th, 2005, 12:08 PM
It depends on the implementation.
You can't have both control and speed.
Thank you all for your quick replies,
D
Siddhartha
September 26th, 2005, 12:13 PM
']It depends on the implementation.
You can't have both control and speed.One can have both control and speed - in fact, control with speed is extremely essential, and speed without control is extremely dangerous.
(Be it a car, or be it a thread.)
Terminating a running thread has its grave dangers -
One does not have a clue regarding the point of code in execution when termination occurs.
One risks not returning resources allocated by the thread.
Objects constructed by the thread will not be destructed.
A simple boolean flag set from outside the thread (and visible inside it) can tell a thread that it must clean-up and return if true - this is fast, and it is safer.
Dan|]
September 26th, 2005, 01:58 PM
To be more specific, suppose we have a worker thread with a not quite simple controlling function. You want to have a dialog in front with a progress bar and a cancel button in order to stop the thread.
Here a flag would be a handle of the dialog. Due to the ctrling function complexity, it is very unusual to put after each line a check on the handle.
I mean, you are right, but due to the complexity of the function, to have things done it is far more esthetical to terminate with the thread.
Besides, if the control it is not 100% perfect, it is very possible to have the thread running after a cancel and this would bring in much more trouble.
Siddhartha
September 26th, 2005, 02:07 PM
if the control it is not 100% perfect, it is very possible to have the thread running after a cancel and this would bring in much more trouble. Well... If you are not ending your threads cleanly, your code is anyways very far from perfect.
I have listed the drawbacks of using TerminateThread - and IMO, they are severe.
Having done the job of conveying the dangers, the path you take is your decision.
There are a lot of applications where threads run UI, or do a lot of processing for long periods of time, and applications that execute plenty of threads all at once and bring them down cleanly.
It is the done thing.
Due to the ctrling function complexity, it is very unusual to put after each line a check on the handle. One does not need to check after every line - but check on logical boundaries. If you are running a loop, a check once per loop should be good enough.
The cleanliness of the implementation is dependent on the design and the programmer. Sometimes, a few extra lines of code that will make your application run cleanly and secure are worth it.
I think you got the point already... ;)
Good luck.
Dan|]
September 27th, 2005, 09:33 AM
Or divide the controlling function in some sort of atoms of execution and have a worker thread for each one.
Thank you once again for your attention onto my humble difficulties,
DD
Siddhartha
September 27th, 2005, 09:34 AM
']Thank you once again for your attention onto my humble difficulties,You are welcome, Dan... ;)
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.