The thread creates the function CreateThread1. Function Prototypes:
HANDLE WINAPI CreateThread( _In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes, _In_ SIZE_T dwStackSize, _In_ LPTHREAD_START_ROUTINE lpStartAddress, _In_opt_ LPVOID lpParameter, _In_ DWORD dwCreationFlags, _Out_opt_ LPDWORD lpThreadId );
2. Parameter Description:
The first parameter lpThreadAttributes
represents the security properties of the thread kernel object, and a general pass-through of NULL means that the default setting is used.
The second parameter dwStackSize
represents the size of the line stacks space. Passing in 0 means using the default size (1MB).
The third parameter lpStartAddress
represents the address of the thread function that the new thread executes, and multiple threads can use the same function address.
The fourth parameter lpParameter
is a parameter passed to the thread function.
The fifth parameter dwCreationFlags
specifies an additional flag to control the creation of the thread, 0 indicates that the thread is ready to be dispatched immediately after it is created, and if create_suspended indicates that the thread is created and paused, it cannot be dispatched until ResumeThread () is called.
The sixth parameter lpThreadId
returns the ID number of the thread, and passing in null means that the thread ID number is not required to be returned.
3. Return value
Thread creation successfully returns a handle to the new thread, failure returns null
Two examples
/* create the first thread. The thread is revoked when the main process ends. */ #include <windows.h> #include <stdio.h> DWORD WINAPI ThreadFunc (LPVOID); void Main () {HANDLE hthread; DWORD threadId; Hthread = CreateThread (NULL, 0 , ThreadFunc, 0 , 0 , &threadid); //create thread printf ( "I am the main thread, PID =%d \n , GetCurrentThreadID ()); //output main thread pid Sleep (2000 );} DWORD WINAPI ThreadFunc (LPVOID p) {printf ( "I am a child thread, PID =%d \n ", GetCurrentThreadID ()); //output sub-thread PID return 0 ;}
The function of this program is very simple, the main thread to create a threads, while the main thread and child threads to the console output the PID of the thread, such as:
Windows multithreading (i) Creating Threads CreateThread