Create a simple thread
HANDLE CreateThread (
_ In_opt _ LPSECURITY_ATTRIBUTES lpThreadAttributes,
// The SECURITY_ATTRIBUTES structure specifies the Security Attribute of this thread. If NULL is entered, the child is created with the default security description, and the returned handle is not inherited.
_ In_SIZE_T dwStackSize,
// The stack size of the thread (in bytes). If it is 0, it is as large as the main thread by default. The stack re-process is automatically allocated in the memory space and released at the end of the process. The stack size can be increased if necessary.
// CreateThread attempts to allocate the memory with a size of dwAtackSize in bytes and returns a failure message when the available memory is insufficient.
_ In_LPTHREAD_START_ROUTINE lpStartAddress,
// Point to the function provided by the application to be executed by the thread, which also represents the address at which the thread starts. The function accepts a 32-bit parameter and returns a 32-bit value.
_ In_opt _ drv_aliasesMem LPVOID lpParameter,
// Specify a 32-bit parameter value passed to the thread
_ In_DWORD dwCreationFlags,
// Specify an additional flag to control thread creation. If the create_suincluded flag is defined, the thread is created in the suspended state, that is, it is not executed until the ResumeThread () function is called.
// If the value is 0, the thread starts execution immediately after creation.
_ Out_opt _ LPDWORD lpThreadId
// Point to the ID of a saving thread, a 32-bit variable
)
If the execution is successful, the return value is the handle pointing to the next new thread. If it fails, NULL is returned.
After a thread is executed, the handle of the thread should be closed. CloseHandle (). This function uses CreateThread () to return the vertex handle, and adds or subtracted 1 to the Reference Counter of the corresponding kernel object.
This does not force a thread to be closed, but tells the system to run when the thread is being modified.
Return true if BOOL CloseHandle (_ In _ HANDLE hObject) is successful. Otherwise, return false.
Test code 1 when the number of thread loops is less than the number of main threads
# Include
# Include
Using namespace std; dword winapi Print_Thread (LPVOID data) {cout <"start thread \ n"; for (int index = 0; index <25; index ++) // 25 cycles {cout <"Thread _" <(int) data <"_" <
# Include
Using namespace std dword winapi Print_Thread (LPVOID data) {cout <"start thread \ n"; for (int index = 0; index <50; index ++) // loop for 50 times {cout <"Thread _" <(int) data <"_" <
I only checked that code 1 may not have any special discoveries, so I wrote code 2 and compared it. Careful readers will find that I only changed the number of times of the main thread and subthread for loop.
By comparing the two codes, you will find that when the main thread ends, the sub-thread continues because I have added system ("pause "); therefore, after the for loop of the first main thread ends, the program does not exit directly. However, we can see that the sub-thread is still running until it ends.If you set system ("Pause"); You will find that when the main thread completes execution, the program will exit directly and will not wait until the sub-thread ends.