Thread Learning Diary (1) 1) Creation of Threads
Create a function - the function on the MSDN
HANDLE CreateThread (LPSECURITY_ATTRIBUTES lpThreadAttributes, SIZE_T dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId); pThreadAttributes first parameter is a function of the security attributes of the security attribute set to the default start NULL second parameter assigned to a thread start dwStackSize The size of the stack page can be set to the default size 0 The entry function of the third parameter lpstartaddress thread can set the DWORD WINAPI FUN1PRO (LPVOID LPPARETER); the fourth is the parameters passing to the thread, the type is a pointer type can be NULL fifth Parameter DWCREATIONFLAGS When you call when you call if it is crete_suspended, the thread does not run when you created until the resumethread function is called if it is 0, then start running after the thread is created; the sixth parameter lpthreadID is used to transfer thread ID if NULL does not return thread ID
Here is a thread program I started.
DWORD WINAPI Fun1Pro (LPVOID lpParameter); void main () {HANDLE hThread1; hThread1 = CreateThread (NULL, 0, Fun1Pro, NULL, 0, NULL); CloseHandle (hThread1); cout << "main thread run" << endl; }
DWORD WINAPI FUN1PRO (LPVOID LPPARETER) {COUT << "Thread2 Is Run" << Endl; Return 0;}
This program is not ideal, that is, when the main thread is completed, the process I created is not running. The result is that the result is: When the main thread is executed, the main thread exits, the main thread exits, then it The process of creation is to exit, this is the reason for Thread2 Is Run;
In order to make the creation can run, always introduce a SLEEP function, call the function at the exit of the main function, allow the main thread to be temporarily running, and then say that the main thread gives up the CPU, so the process of creation is Opportunity executed void main () {handle hthread1; hthread1 = Createthread (NULL, 0, FUN1PRO, NULL, 0, NULL); CloseHandle (HTHREAD1); COUT << "Main Thread Run" <<
Sleep (10);