In VC, there are three ways to start the first-line process:
1. Enable a thread using MFC. The afxbeginthread function is generally used;
2. createthread is used for API usage;
3. Use the _ beginthread and _ beginthreadex functions.
Use the afxbeginthread function of MFC to start a thread. Whether the input parameter is a thread class inherited from cwinthread or a standard thread function address and parameter (uint threadproc (lpvoid pparam) and returns a cwinthread pointer to facilitate our use. However, no one has carefully analyzed the mechanism in this book, and there is no way to view the MFC source code. After analysis, we will share the analysis results with you. :)
There are two constructors in the cwinthread class:
One is the default construction, and the other is the standard thread function address and the input parameter constructor-
Cwinthread (afx_threadproc pfnthreadproc, lpvoid pparam ). When we start a thread using afxbeginthread, there are no other two methods:
1. Directly start the Thread class of the cwinthread base class-afxbeginthread (runtime_class (...),...);
2. Directly start the standard thread function-afxbeginthread (afx_threadproc), lpvoid ,....);
Another method that may not be commonly used is to inherit a Thread class with cwinthread, new in the memory, and then call the member function createthread of cwinthread to start itself;
After my analysis, in fact, 1st and 2nd methods are the details of the methods in 3rd, which is more flexible to call, but the comparison between 1 and 2 is more convenient. :)
In fact, afxbeginthread creates a cwinthread Based on the parameter implementation: If the parameter is a standard thread function address, a new cwinthread object is constructed based on the function address and parameter; if the parameter is a class inherited by cwinthread, call the Createobject of cruntimeclass to create a cwinthread object, which is constructed using the default parameter. Then, call the createthread member function. If the call fails, call Delete () to determine whether the self-deleted object is set (m_bautodelete = true). If it is true, delete itself.
In the createthread function, first create two events. MFC uses _ beginthreadex to start a global thread function named _ afxthreadentry and uses the _ afx_thread_startup struct to pass in a series of parameters, wait for _ afxthreadentry to initialize a thread and set the event 1 State. Then, set another event 2 state to trigger the code in _ afxthreadentry. In _ afxthreadentry, the function first initializes the thread through a series of functions, and then sets Event 1 and waits for event 2 after the createthread of cwinthread is executed. Then, it judges whether the thread function address in _ afx_thread_startup is empty, if it is not empty, the function code is directly executed. If it is not empty, the initinstance of cwinthread is executed to determine whether the return value is true. If yes, the system enters the message loop until the message is sent to the wm_quit thread; if the returned value is false, the exiti Nstance; afxendthread is called to end the thread. Afxendthread obtains the cwinthread * type pointer from the module to determine whether it is null. If not, it calls the cwinthread Delete to delete the cwinthread from the memory.
The following is a simple flowchart:
Afxbeginthread:
Afxbeginthread Process
_ Afxthreadentry Process
Hope to be useful to everyone!