To kill a thread
-
I am calling a function in a thread that contains an infinite while loop and I want to stop that loop and as well as kill/exit thread on some condition. Can anyone please tell me how to do that?
Regards, Mushq
-
I am calling a function in a thread that contains an infinite while loop and I want to stop that loop and as well as kill/exit thread on some condition. Can anyone please tell me how to do that?
Regards, Mushq
Use some kind of signalling mechanism between the thing that detects the termination condition and the thread. That infinite loop should have a check at the top for the condition. Easiest choice in windows is to use an event. Only use TerminateThread as an absolute last resort, only if you can't do it some other way. I've been multithreading for over 20 years and I have never had to use TerminateThread. It leaves your system in a mess, with all sorts of resources still allocated. Judy
-
I am calling a function in a thread that contains an infinite while loop and I want to stop that loop and as well as kill/exit thread on some condition. Can anyone please tell me how to do that?
Regards, Mushq
-
Use some kind of signalling mechanism between the thing that detects the termination condition and the thread. That infinite loop should have a check at the top for the condition. Easiest choice in windows is to use an event. Only use TerminateThread as an absolute last resort, only if you can't do it some other way. I've been multithreading for over 20 years and I have never had to use TerminateThread. It leaves your system in a mess, with all sorts of resources still allocated. Judy
JudyL_FL wrote:
I've been multithreading for over 20 years
Come on, Judy...since you were a toddler? I'm not buying it ;)
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
JudyL_FL wrote:
I've been multithreading for over 20 years
Come on, Judy...since you were a toddler? I'm not buying it ;)
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
TerminateThread will do the job, if you're not concerned about cleanup/releasing of resources.
BOOL WINAPI TerminateThread(
__inout HANDLE hThread,
__in DWORD dwExitCode
); -
I think its second times that somebody told about use of TerminateThread and you reply to him or her why its bad with a link. :-D
-
I think its second times that somebody told about use of TerminateThread and you reply to him or her why its bad with a link. :-D
Yeah, That sounds about right. It's one of those classic mistakes that even "professional" programmers make over and over again.
Steve
-
Yeah, That sounds about right. It's one of those classic mistakes that even "professional" programmers make over and over again.
Steve
I personality like to read your replys because they arecomplete and helpful.
-
I personality like to read your replys because they arecomplete and helpful.
Thank you.
Steve