Parse how to terminate the running of threads in C/C ++

Source: Internet
Author: User

To terminate the running of a thread, use the following methods:
1. thread function return (this method is recommended ).
2. By calling the ExitThread function, the thread will undo it by itself (it is best not to use this method ).
3. The TerminateThread function is called by threads in the same process or another process (this method should be avoided ).
4. The ExitProcess and TerminateProcess functions can also be used to terminate the running of a thread (this method should be avoided ).

The following describes how to terminate a thread in detail: 1-4, and what happens when the thread stops running: 5.

1. thread function return
Threads should always be designed in this form, that is, they can be returned when they want to terminate the operation. This is the only way to ensure that all thread resources are correctly cleared.
If the thread can return, the following items can be ensured:
(1) All C ++ objects created in the thread functions are correctly revoked through their undo functions.
(2) the operating system correctly releases the memory used by the thread stack.
(3) The system sets the exit code of the thread (maintained in the kernel object of the thread) as the return value of the thread function.
(4) The system will decrease the usage count of the thread kernel object.

2. ExitThread Function
The thread can call the ExitThread function to force the thread to terminate the operation:
Function prototype:
VOID ExitThread (DWORD dwExitCode );
This function terminates the running of the thread and causes the operating system to clear all operating system resources used by the thread. However, C ++ resources (such as C ++ class objects) are not revoked. For this reason, it is best to return data from the thread function instead of calling ExitThread.
Of course, you can use the dwExitThread parameter of ExitThread to tell the system why to set the exit code of the thread. The ExitThread function does not return any value because the thread has been terminated and cannot execute more code.
Note that the best way to terminate a thread is to let its thread function return.However, if you use the method described in this section, you should know that the ExitThread function is a function used by Windows to cancel a thread. If you write C/C ++ code, you should never call ExitThread. You should use the Visual C ++ Runtime library function _ endthreadex. If you do not use Microsoft's Visual C ++ compiler, your compiler vendor has its own ExitThread substitution function. No matter what this alternative function is, it must be used.

3. TerminateThread Function
Calling the TerminateThread function can also terminate the running of the thread:
Function prototype:

Copy codeThe Code is as follows: BOOL TerminateThread (
HANDLE hThread,
DWORD dwExitCode );

Unlike ExitThread, ExitThread always revokes the calling thread, while TerminateThread can undo any thread. The hThread parameter is used to identify the handle of the terminated thread. When the thread stops running, its exit code becomes the value you pass as the dwExitCode parameter. At the same time, the usage count of the kernel object of the thread is also decreased.
Note that the TerminateThread function is a function that runs asynchronously.That is to say, it tells the system that you want the thread to terminate the operation, but when the function returns, the thread cannot be undone. If You Need To Know exactly that the thread has been terminated, you must call WaitForSingleObject or similar functions to pass the thread handle.
Well-designed applications never use this function, because the terminated thread cannot receive the Undo notification. The thread cannot be cleared correctly and cannot be undone.
Note that the memory stack of a thread is also revoked when the thread is revoked by returning or calling the ExitThread method.However, if TerminateThread is used, the system will not undo the stack of the thread before the process with the thread stops running. Microsoft deliberately uses this method to implement TerminateThread. If other threads that are still executing need to reference the value on the thread stack of force revocation, other threads will encounter access violations. If you leave the stack of the withdrawn thread in the memory, other threads can continue to run well.
In addition, when the thread stops running, the DLL usually receives a notification. If you use TerminateThread to force the thread to terminate, the DLL will not receive notifications, which can prevent proper cleanup.

4. Cancel the thread when the process stops running
The ExitProcess and TerminateProcess functions can also be used to terminate the running of a thread. The difference is that these threads will terminate all the threads in the terminated processes. In addition, because the entire process has been disabled, all resources used by the process must have been cleared. Of course, this includes the stacks of all threads. These two functions cause the remaining threads in the process to be forcibly revoked, just like calling TerminateThread from each remaining thread. Obviously, this means that the correct application clearing does not happen, that is, the C ++ object Undo function is not called, and the data is not transferred to the disk.

5. Operations that occur when the thread stops running
When the thread stops running, the following operations occur:
(1) All user objects owned by the thread are released. In Windows, most objects are owned by processes that contain threads that create these objects. However, a thread has two user objects, window and hook. When the thread stops running, the system will automatically undo any windows and uninstall any hooks created or installed by the thread. Other objects are revoked only when a thread-owning process stops running.
(2) Change the exit code of the thread from STILL_ACTIVE to the Code passed to ExitThread or TerminateThread.
(3) The state of the thread kernel object has changed to notified.
(4) If the thread is the last active thread in the process, the system regards the process as terminated.
(5) The usage count of the thread kernel object is reduced by 1.
When a thread stops running, the kernel object will not be automatically released until all unfinished references of the thread kernel object associated with it are closed.
Once the thread no longer runs, no other thread in the system can process the handle of the thread. However, other threads can call GetExitcodeThread to check whether the thread identified by the hThread has been terminated. If it has been terminated, determine its exit code:
Function prototype:

Copy codeThe Code is as follows: BOOL GetExitCodeThread (
HANDLE hThread,
PDWORD pdwExitCode );

The exit code value is returned in DWORD pointed to by pdwExitCode. If the thread has not ended running when GetExitCodeThread is called, this function uses the STILL_ACTIVE identifier (defined as 0x103) to fill in DWORD. If the function runs successfully, TRUE is returned.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.