Depth analysis of MFC threads and mechanisms

zhaozj2021-02-11  218

In VC, there are three ways to start a thread:

1. Start a thread in the MFC to generally use the AFXBEGINTHREAD function;

2. Use the API to use CreateThread;

3. That is to use _beginthread and _beginthreadex two functions.

Use the MFC's AFXBEGINTHREAD function to launch a thread, whether or not your parameters are a thread class that is inherited from CwinThread or a standard thread function address and parameter (Uint ThreadProc (LPVoid PParam), a CWINTHREAD pointer is returned. It is convenient for us to use. But where there is no one in which the book is carefully analyzed, there is no way to view the MFC source code. After analysis, the results of the analysis are now shared with everyone. :)

There are two constructor in the CWINTHREAD class:

One is the default constructor, and there is a constructor for a standard thread function address and the input parameters - CwinThread (Afx_ThreadProc PfnthreadProc, LPVOID PPARAM). When we use AFXBeginThread to start a thread, there is no external method: 1. Start the CWINTHREAD for the thread class - Afxbeginthread (Runtime_class (...), ...); 2. Launch the standard thread function -AFXBEGINTHREAD ((AFX_THREADPROC), LPVOID, ....);

There is also a way to use CwinThread inheriting a thread class, in the memory, then call CwinThread member function CreThread to start itself; after my analysis, the first and second method is 3 The detailed details of the Chinese method, more flexible, but 1, 2, more convenient than it. :) In fact, AFXBEGINTHREAD creates a CWINTHREAD thread according to the parameter: If the parameter is a standard thread function address, the new CWINTHREAD object is constructed in the function address and the parameter; if the parameter is a class of CwinThread, CruntimeClass is called directly. CreateObject creates a CWINTHREAD object to construct the default parameter. Then call the CreateThread member function, call the unsuccessful call delete (), and determine if you set a self-deleting object (m_bautode = true), delete itself for true.

In the CreateThread function, you first create 2 events, MFC uses _beginthreadex to start a full-end thread function to _afxthreadentry, and passed a series of parameters through a _afx_thread_startup structure, then wait _afxthreadentry to initialize a thread initialization Event 1 state, then set another event 2 status, trigger the code in _afxThreadEntry; in _afxthreadENTRY, the function first initials the thread through a series of functions, and then set the event 1 Status After waiting for the CREATTHREAD of CWINTHREAD. 2. At this time, it is empty if the thread function address in _afx_thread_startup is empty. If you do not directly execute the function code; if you do not empty, you will perform the cwinthread's initInstance to determine whether it is returned to true, if you enter the message loop know to send it to Thread wm_quit messages; if returned to false, call EXITINSTANCE; finally call the AFXEndThread end thread. AFXENDTHREAD determines whether the cwinthread * type of the cwinthread * type is determined from the module, no, call CWINTHREAD's delete to remove CwinThread from memory. The following is a simple flow chart:

AfxBeginthread:

AFXBEGINTHREAD process

_AFXTHREADENTRY process

hope its good for U.S!

转载请注明原文地址:https://www.9cbs.com/read-4274.html

New Post(0)