Windows SDK Thread: How to end a thread?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1

Thread: Windows SDK Thread: How to end a thread?

  1. #1
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Threads: How to end a thread?

    Q: How to end a thread?

    A: There are different ways depending whether the thread will be ended from inside or outside. For ending a thread from inside the function 'ExitThread()' can be used.

    Code:
    ExitThread(0);    // Zero is the exit code in this example
    This will end the actual thread. It is the preferred method to end a thread. This function will also be called implicitly while returning from the thread procedure and provides a clean shutdown of the thread.

    To end a thread from outside there exist also two general possibilities. To provide a clean exit of the thread an event can be set which was passed as a thread parameter and which is frequently checked by the thread procedure.

    Code:
    class CFoo
    {
    public:
      CFoo();
      ~CFoo();
    
    private:
      HANDLE m_StopThread;
      HANDLE m_WaitThread;
    
      static UINT ThreadFunction(LPVOID pvParam);
    };
    
    CFoo::CFoo()
    {
      // Create events
      m_StopThread = CreateEvent(0, TRUE, FALSE, 0);
      m_WaitThread = CreateEvent(0, TRUE, FALSE, 0);
    
      // Start thread
      AfxBeginThread(ThreadFunction, this);
    }
    
    CFoo::~CFoo()
    {
      // Trigger thread to stop
      ::SetEvent(m_StopThread);
    
      // Wait until thread finished
      ::WaitForSingleObject(m_WaitThread, INFINITE);
    
      // Close handles
      ::CloseHandle(m_StopThread);
      ::CloseHandle(m_WaitThread);
    }
    
    UINT CFoo::ThreadFunction(LPVOID pvParam)
    {
      CFoo *pParent = static_cast<CFoo*>(pvParam);
    
      while(true)
      {
        // Check event for stop thread
        if(::WaitForSingleObject(pParent->m_StopThread, 0) == WAIT_OBJECT_0)
        {
          // Set event
          ::SetEvent(pParent->m_WaitThread);
    
          return 0;
        }
    
        // Do your processing
      }
    }
    If the approach described above is not possible for several reasons (e.g. a lengthy database query is performed) then the thread can be forced to exit by a call to 'TerminateThread()'.

    Code:
    if(TerminateThread(hThread, 0) == FALSE)
      // Could not force thread to exit -> call 'GetLastError()'
    'hThread' is the handle of the thread which can be obtained by use of any of the functions described in 'How to create a worker thread?'. On Windows NT, Windows 2000 and Windows XP systems, the handle must have 'THREAD_TERMINATE' access.

    'TerminateThread()' has some drawbacks which should be considered. It causes the thread to exit. When this occurs, the thread does not have the chance to execute any user-mode code any longer. DLLs attached to the thread are not notified that the thread is terminating.

    Additional information is provided by the MSDN:

    TerminateThread is a dangerous function that should only be used in the most extreme cases. You should call TerminateThread only if you know exactly what the target thread is doing, and you control all of the code that the target thread could possibly be running at the time of the termination. For example, TerminateThread can result in the following problems:
    • If the target thread owns a critical section, the critical section will not be released.
    • If the target thread is allocating memory from the heap, the heap lock will not be released.
    • If the target thread is executing certain kernel32 calls when it is terminated, the kernel32 state for the thread's process could be inconsistent.
    • 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.
    A thread cannot protect itself against TerminateThread, other than by controlling access to its handles. The thread handle returned by the CreateThread and CreateProcess functions has THREAD_TERMINATE access, so any caller holding one of these handles can terminate your thread.
    Last edited by cilu; October 26th, 2007 at 04:21 AM. Reason: removed incorrect pointer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center