|
-
June 2nd, 2010, 03:01 PM
#1
The problem with TerminateThread
I heard that a disadvantage by calling a TerminateThread to terminate a thread is that when the thread ends, the system doesn't notify any DLLs attached to the process owning the thread. I don't see any problems with that. Can any guru here explain what the potential problems are there? Thanks for your inputs.
-
June 2nd, 2010, 04:36 PM
#2
Re: The problem with TerminateThread
Its an interesting question. Ill be watching for guru's replies, this can be quite a detailed topic.
How do you know the DLL doesn't do some essential cleanup when its unloaded? There are numerous situations where it could cause problems. There are also other issues such as are local objects wont have their destructors called. If you will specifically get problems doing this or not depends on your application however, it is deemed bad practice.
Are you specifically wanting information about the DLL's not being notified or are you wanting information generally on why its a bad idea?
My question to you is why would you call TerminateThread? Its extremely easy to avoid (up until the point of last resort) and it will insure everything "plays nice".
Something like the following, most error checking left out but will give general idea:
Code:
... myThread(void * pData)
{
bool bRun = true;
while (bRun)
{
if (::WaitForSingleObject(hThreadClose, 50) == WAIT_OBJECT_0)
{
bRun = false;
}
else
{
// Do somthing.
}
}
return 0;
}
void CloseThread()
{
::SetEvent(hThreadClose);
if (::WaitForSingleObject(hThread, 2000) != WAIT_OBJECT_0)
::TerminateThread(hThread);
hThread = 0;
}
Regards
Last edited by UnfitElf; June 2nd, 2010 at 04:51 PM.
Learning somthing new every day! 
-
June 3rd, 2010, 05:19 PM
#3
Re: The problem with TerminateThread
 Originally Posted by dullboy
I heard that a disadvantage by calling a TerminateThread to terminate a thread is that when the thread ends, the system doesn't notify any DLLs attached to the process owning the thread. I don't see any problems with that. Can any guru here explain what the potential problems are there? Thanks for your inputs.
That is certainly one danger in use of TerminateThread. But the documentation for this function lists many other dangers. Can you find them? Do you understand them?
Last edited by MikeAThon; June 4th, 2010 at 04:24 PM.
-
June 3rd, 2010, 11:01 PM
#4
Re: The problem with TerminateThread
I know there is another danger related to thread stack. Could you explain the danger I mentioned in my first post? Thanks for your inputs.
 Originally Posted by MikeAThon
That is certainly one danger in use of TerminateThread. But the documentation for this function lists many other dangers. Can you them? Do you understand them?
-
June 4th, 2010, 02:52 AM
#5
Re: The problem with TerminateThread
Victor Nijegorodov
-
June 4th, 2010, 05:14 AM
#6
Re: The problem with TerminateThread
It is a nice article. Thank you. This article mentioned that "If the target thread is manipulating the global state of a shared DLL, the state of the DLL could be destroyed, affecting other users of the DLL. ". I wander what exactly global state of a shared DLL is? Can you give me an example? Also I have another question. When a thread is completing a lengthy operation, usually we can call TerminateThread to terminate the thread, I wander in that case, can we still use ExitThread to terminate the thread? Thanks for your inputs.
 Originally Posted by VictorN
-
June 4th, 2010, 05:42 AM
#7
Re: The problem with TerminateThread
 Originally Posted by dullboy
When a thread is completing a lengthy operation, usually we can call TerminateThread to terminate the thread, I wander in that case, can we still use ExitThread to terminate the thread? Thanks for your inputs.
Usually, we must not call TerminateThread at all.
Thread must exit itself (by return from a thread procedure in the case of a worker thread or by calling PostQuitMessage(0) in the case of UI thread)
Victor Nijegorodov
-
June 4th, 2010, 05:46 AM
#8
Re: The problem with TerminateThread
 Originally Posted by dullboy
... what exactly global state of a shared DLL is? Can you give me an example?
Googly can give you a lot of information
Victor Nijegorodov
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|