VC-terminate thread, exitthread function, te r m I n a t e t h r e a D function, cancel thread, details

Source: Internet
Author: User

VC-terminate thread, exitthread function, te r m I n a t e t h r e a D function, cancel thread, details

6.5 terminate the running of a thread
To terminate the running of a thread, use the following method:
• Thread function return (this method is recommended ).
• By calling the e x I t h r e a D function, the thread will undo it by itself (it is best not to use this method ).
• Threads in the same process or in another process call the Te r m I n a t e t h r e a D function (this method should be avoided ).
• Process containing threads stops running (this method should be avoided ).
The following describes how to terminate a thread and what happens when the thread stops running.
6.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:
• All C ++ objects created in the thread functions are correctly revoked through their undo functions.
• The operating system correctly releases the memory used by the thread stack.
• The system exits the thread. Code (Maintained in the kernel object of the thread) is set to the return value of the thread function.
• The system will decrease the usage count of the thread kernel object.
6.5.2 exitthread Function
The thread can call the e x I t h r e a D function to force the thread to terminate the operation:
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 from the thread function, rather than by calling e x I t h r e a D (for details, see chapter 4th ).
Of course, you can use the d w e x I t h r e a d parameter of e x I t h r e a d to tell the system why to set the thread exit code. E x I t h r e a D function does not return any value, because the thread has 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 E x I t h r e a D function is a function that wi n d o W S is used to cancel the thread. If you write C/C ++ code, you should never call e x I t h r e a D. Visual c ++ Runtime library function _ e n d t h r e a d e X should be used. If the Visual C ++ compiler of m I c r o s o f t is not used, your compiler vendor has its own alternative e x I t h r e a D function. No matter what this alternative function is, it must be used. This chapter describes the functions and importance of _ e n t h r e a d e x.
6.5.3 te r m I n a t e t h r e a d Function
Calling the Te r m I n a t e t h r e a D function can also terminate the thread operation:
Chapter 1 Basic thread knowledge 6th
Unlike E x I t h r e a d, e x I t h r e a d always revokes the calling thread, te r m I n a t e t h r e a D can cancel any thread. The h t h r e a d 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 d w e x I t C o d e parameter. At the same time, the usage count of the kernel object of the thread is also decreased.
Note that the Te r m I n a t e t h r e a D function is a function that runs asynchronously, that is, it tells the system that you want the thread to terminate the operation, but when the function returns, the thread cannot be canceled. If You Need To Know exactly that the thread has been terminated, you must call wa I t f o r s I n g l e o B j e c t (Chapter 1) or similar function, passing the thread's
Handle.
Well-designed applications Program This function is never used 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 the thread is also revoked when the thread is revoked by returning or calling the e x I t h r e a D method. However, if you use te r m I n a t e t h r e a D, the system does not undo the stack of the thread before the process that owns the thread stops running. M I c r o s o f t deliberately uses this method to implement Te r m I n a t e t h r e a D. 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, d l generally receives notifications. If you use the terminate thread to force the thread to terminate, d l will not receive notifications, which can prevent proper cleanup (for details, see chapter 0 ).
6.5.4 cancel a thread when the process stops running
The E x I t p r o c e s and Te r m I n a T E P R o C E S functions described in Chapter 4th can also be used to terminate the running of threads. 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 te r m I n a t e t h r e a D 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.

Windows core programming

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.