Handle use of Threads:
Instead of pointing to the thread itself, the thread's handle is pointing to the thread's kernel object. Each kernel object is just a block of memory allocated by the kernel and is accessible only by the kernel. The memory block is a data structure whose members are responsible for maintaining various information about the object (eg: security description, reference count, etc.).
CloseHandle ()
After CreateThread succeeds, it returns a hthread handle, and after the count of the kernel object is added 1,closehandle, the reference count is reduced by 1, and when it becomes 0 o'clock, the system deletes the kernel object.
But this handle does not fully represent this thread, it is just an "identity" of the thread, and the system and the user can use it to manipulate the corresponding thread as necessary. If the online Cheng is created, it is no longer necessary to use the handle, it can be closehandle directly before the thread exits after the successful creation, but this does not affect the thread's operation.
Consequences of not performing CloseHandle ():
If the thread is executed, the reference count is not reduced by 1 by CloseHandle (), and the kernel object is compromised during process execution, rather than the handle, but differs from the memory leak, which is bound to have some negative effect on the efficiency of the system. However, keep in mind that when the process finishes exiting, the system will still automatically help you clean up those resources. But this is not recommended here, after all, not a good programming habit!
(It is possible for the application to leak kernel objects while it is running, but when the process terminates, the system ensures that everything is properly purged.) In addition, this condition is used for all objects, resources, and blocks of memory, that is, when the process terminates, the system guarantees that no objects will be left. )
TerminateThread ()
The declaration of the function is as follows:
BOOL TerminateThread (HANDLE hthread, DWORD dwexitcode);
Role:
Terminates a thread out of line to force the termination of the thread.
Parameter description:
HANDLE Htread: The handle of the terminated thread, for the CWinThread pointer.
DWORD Dwexitcode: Exit code.
return value:
The function execution succeeds returns a value other than 0, and the execution failure returns 0. Call GetLastError to get the returned value.
Listen to countless times do not terminatethread, but the work is often used, it seems that there is no problem. An unforgivable mistake was found today in the high-intensity test. See the example below
[CPP] View plaincopy
DWORD __stdcall Mythread (void*)
{
while (true)
{
char* p = new char[1024];
delete [] p;
}
}
int _tmain (int argc, _tchar* argv[])
{
HANDLE h = createthread (null, 0, mythread, NULL, 0, NULL);
Sleep (1000);
TerminateThread (h, 0);
h = NULL;
char* p = new char[1024]; It's going to be a dead lock.
delete [] p;
return 0;
}
Why the deadlock? The new operator uses a small heap, and the entire process uses the same lock when allocating and reclaiming memory. If a thread is killed while it is occupying the lock (that is, the thread is in a new or delete operation before dying), the other thread can no longer use new or delete, which is displayed as hang.
< core programming > is clearly reminded not to terminatethread, but the reason is not the bloody drop. The bug found today confirms the value of the book.
Another note: Many temporary network operations often use TerminateThread, as the network when the exit mechanism, to be changed later. For example, let the thread fend for itself and exit itself.
ExitThread is the recommended way to end a thread, when the function is called, the current thread's stack is freed, and then the thread terminates, which is better than the TerminateThread function to complete the cleanup of the DLLs attached to the thread
Terminating a thread two functions: ExitThread () and TerminateThread ()
To terminate a thread's operation, you can use the following four ways:
The thread function exits the loop to return (the best method).
By calling the ExitThread function, the thread will undo itself (try not to use this method).
The TerminateThread function is called by the same process or a thread in another process (it is best to avoid using this method).
The main process of the thread terminates (avoids use).
The methods that terminate the thread run are described below, and what happens when the thread terminates the run.
1. Thread function return
Threads should always be designed in such a way that they can return when they want the thread to terminate the run. This is
The only way to ensure that all thread resources are properly purged.
If the thread is able to return, you can ensure that the following things are implemented:
A) all C + + objects created in the thread function are correctly undone through their undo functions.
b) The operating system will correctly release the memory used by the thread stack.
c) The system sets the exit code (maintained in the kernel object of the thread) to the return value of the thread function.
d) The system decrements the usage count of the thread kernel object.
The
2.ExitThread function
allows a thread to invoke the ExitThread function to force the thread to terminate running:
The function terminates the running of the thread and causes the operating system to purge all operating system resources used by that thread. However, C + + resources, such as C + + class objects, will not be undone. For this reason, it is best to return from the thread function rather than by calling ExitThread.
Of course, you can use the Dwexitthread parameter of exitthread to tell the system why the thread's exit code is set. The ExitThread function does not return any values because the thread has terminated and cannot execute more code.
Note that the best way to terminate a thread's operation is to have its thread function return. However, if you use the methods described in this section, you should know that the ExitThread function is a function that Windows uses to undo threads. You should never call ExitThread if you are writing C + + code. You should use the Visual C + + run-time library function _endthreadex. If you do not use Microsoft's Visual C + + compiler, your compiler vendor has its own exitthread substitution function. Whatever this substitution function is, it must be used. The role of _endthreadex and its importance are explained later in this chapter.
The
3.TerminateThread function
calls the TerminateThread function to also terminate the thread's run:
differs from exitthread ,exitthread always undoes the calling thread. And terminatethread can undo any thread. The Hthread parameter identifies the handle to the thread that is being terminated. When the thread terminates running, its exit code becomes the value that you pass as the dwexitthread parameter. At the same time, the usage count of the thread's kernel object is decremented. The
Note that the TerminateThread function is a function that runs asynchronously, that is, it tells the system that you want the thread to terminate the run, but when the function returns, there is no guarantee that the thread will be undone. If you need to know exactly if the thread has been terminated, you must call WaitForSingleObject or a similar function to pass a handle to the thread. The
well-designed application never uses this function because the thread that is terminated does not receive the notification that it was revoked. The thread does not clear properly and does not prevent itself from being undone. Note When you use a method that returns or calls exitthread to undo a thread, the memory stack for that thread is also undone. However, if you use TERMINATETHREAD&NBSP, the system does not undo the thread's stack until the process that owns the thread terminates. Microsoft intentionally uses this approach to implement terminatethread . If other threads that are still executing are referencing the values on the thread stack that is forced to undo, then other threads will have an access violation problem. If you leave the stack of a thread that has been undone in memory, other threads can continue to run well. In addition, DLLs usually receive notifications when a thread terminates running. If the thread is forced to terminate using the terminate thread, the DLL will not receive notifications, which will prevent proper cleanup (see Chapter 20th for details).
4. Undoing a thread when the process terminates its run
The ExitProcess and TerminateProcess functions can also be used to terminate the running of a thread. The difference is that these threads will cause all threads in the process terminating to run to terminate. Also, because the entire process has been closed, all resources used by the process must have been purged. This, of course, includes the stack for all threads. These two functions cause the remaining threads in the process to be forcibly undone, just like calling TerminateThread from each remaining thread. Obviously, this means that the correct application cleanup did not occur, that is, the C + + object Undo function was not called, the data did not go to disk, and so on.
The difference between CloseHandle (), TerminateThread (), ExitThread ()