CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2000
    Posts
    1,471

    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.

  2. #2
    Join Date
    Oct 2005
    Posts
    230

    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!

  3. #3
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: The problem with TerminateThread

    Quote Originally Posted by dullboy View Post
    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.

  4. #4
    Join Date
    Aug 2000
    Posts
    1,471

    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.
    Quote Originally Posted by MikeAThon View Post
    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?

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: The problem with TerminateThread

    Did you read this FAQ?
    Victor Nijegorodov

  6. #6
    Join Date
    Aug 2000
    Posts
    1,471

    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.
    Quote Originally Posted by VictorN View Post
    Did you read this FAQ?

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: The problem with TerminateThread

    Quote Originally Posted by dullboy View Post
    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

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: The problem with TerminateThread

    Quote Originally Posted by dullboy View Post
    ... 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
  •  





Click Here to Expand Forum to Full Width

Featured