AFXBEGINTHREAD function

xiaoxiao2021-03-06  37

When multi-threaded program is designed, we often use the AFXBEGINTHREAD function to launch a thread.

This function is very simple and convenient, which is defined as follows.

CWinThread * AfxBeginThread (AFX_THREADPROC pfnThreadProc, // thread function address LPVOID pParam, // thread parameters int nPriority = THREAD_PRIORITY_NORMAL, // thread priority UINT nStackSize = 0, // thread stack size, the default is 1M DWORD dwCreateFlags = 0, // LPSecurity_Attributes LPSecurityATTRS = NULL);

CWinThread * AfxBeginThread (CRuntimeClass * pThreadClass, int nPriority = THREAD_PRIORITY_NORMAL, UINT nStackSize = 0, DWORD dwCreateFlags = 0, LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL);

Parameter Description: PFNTHREADPROC: The address of the thread function cannot be set to null, the thread function must define a static member function of a full-class function or class, for example: uint mythreadfunc (lpvoid lparam) or class a {public: static uint __stdcall mythreadfunc (LPVOID LPARAM);} To define a static member function that is classified, because the static member function of the class does not belong to a class object, so it does not have to pass an additional THIS pointer when the function is called.

PthreadClass: Runtime_class pointing from the subclass object derived from Cwinthread

PPARAM: Parameters to pass to thread function

NPRIORITY: The priority of the thread to be launched, the default priority is Thread_Priority_NORMAL (normal priority), please refer to the Platform SDK SetThreadPriority function Description for detailed description of thread priority

NSTACKSIZE: The stack size of the new thread, if set to 0, use the default size, in general, the default stack size of the thread is 1m in the application

DWCREATEFLAGS: Thread creation flag, which can be specified as the following flag CREATE_SUSPENDED: Start thread in a hang mode, if you want to initialize some member variables in some Cwinthread classes before thread, such as M_Bautodelete or member variables in your derived class When the initialization is complete, you can use the CWINTHREAD class RESUMETHREAD member function to restore the running of the thread. If set to 0, it means immediate startling thread LPSecurityATTRS: pointing to the security descriptor, if you use the default security level As long as you use the default security level This parameter is set to NULL!

The above is a brief description of the AFXBEGINTHREAD function. When we are in use, as long as the first two parameters are specified, other parameters can use the default value. Well, indeed, it is very simple, as long as this function is called, Created a thread. But have you ever thought that the AFXBEGINTHREAD function is how to start thread? How is its internal implementation?

Let's take a look at the internal implementation of the AFXBEGINTHREAD function.

// start the worker thread CWinThread * AFXAPI AfxBeginThread (AFX_THREADPROC pfnThreadProc, LPVOID pParam, int nPriority, UINT nStackSize, DWORD dwCreateFlags, LPSECURITY_ATTRIBUTES lpSecurityAttrs) {# ifndef _MT pfnThreadProc; pParam; nPriority; nStackSize; dwCreateFlags; lpSecurityAttrs; return NULL; #else ASSERT (pfnthreadproc! = null);

CWINTHREAD * pthread = debug_new cwinthread (pfnthreadproc, pparam); assert_valid (pthread);

if | {pThread-> Delete (); return NULL;} (pThread-> CreateThread (dwCreateFlags CREATE_SUSPENDED, nStackSize, lpSecurityAttrs)!) VERIFY (pThread-> SetThreadPriority (nPriority)); if ((dwCreateFlags & CREATE_SUSPENDED)!) VERIFY (pthread-> resumeThread ()! = (dword) -1);

Return pthread; #ENDIF / /! _ MT)}

// start the UI thread CWinThread * AFXAPI AfxBeginThread (CRuntimeClass * pThreadClass, int nPriority, UINT nStackSize, DWORD dwCreateFlags, LPSECURITY_ATTRIBUTES lpSecurityAttrs) {# ifndef _MT pThreadClass; nPriority; nStackSize; dwCreateFlags; lpSecurityAttrs;

Return null; #else Assert (pthreadclass! = null); assert (pthreadclass-> isderivedfrom (runtime_class (cwinthread)));

CWINTHREAD * pthread = (cwinthread *) pthreadclass-> createObject (); if (pthread == null) AFXTHROWMEMORYEXCEPTION (); assert_valid (pthread);

pThread-> m_pThreadParams = NULL; if (pThread-> CreateThread (dwCreateFlags | CREATE_SUSPENDED, nStackSize, lpSecurityAttrs)!) {pThread-> Delete (); return NULL;} VERIFY (pThread-> SetThreadPriority (nPriority)); if (! (DWCREATEFLAGS & CREATE_SUSPENDED) Verify ()! = (dword) -1); return pthread; #ENDIF / /! _ MT}

It can be seen from the above code to have the following things to have the following points:

1. Configure a new CWinThread objects in the heap (worker thread) as Code: CWinThread * pThread = DEBUG_NEW CWinThread (pfnThreadProc, pParam); CreateObject call CRuntimeClass structure function to create objects CWinThread CWinThread * pThread = (CWinThread *) pThreadClass- > CreateObject (); CRUNTIMECLASS, and internal implementation of the MFC-related class, please refer to "In-depth Shallow MFC" Houjie

2. Call CWINTHREAD :: CreateTHREAD () and set the property to generate pthread-> CreateThread (dwcreateflags | create_suspend,, nstacksize, lpsecurityattrs);

3. Set the priority of thread pthread-> setthreadpriority (nPriority);

4. Call CWINTHREAD:: ResumeThreadPthread-> ResumeThread (); Through the above instructions, I think everyone has done inside the inside, and there should be a preliminary understanding! For VC veterans, this article may There is nothing to read, but for beginners, there is still a certain value! In short, I hope this article can give you a little help!

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

New Post(0)