This article describes the VC CWinThread class and and CreateThread API difference analysis, share for everyone reference. The specific analysis is as follows:
CWinThread
CObject
└ccmdtarget
└cwinthread
The CWinThread object represents a thread that runs within an application. The main thread that is running is usually provided by CWinApp derived classes; CWinApp is derived from CWinThread. In addition, the CWinThread object allows a given application to have multiple threads.
CWinThread supports two types of threads: worker threads (worker thread) and user interface thread (UI thread). Worker threads do not have the ability to send and receive messages (no Message Queuing): For example, a thread that is in the background calculation in a spreadsheet application.
The user interface thread has the ability to send and receive messages, and to handle messages received from the system. CWinApp and its derived classes are examples of user interface threads. Other user interface threads can also be derived directly from CWinThread.
Objects of the CWinThread class exist in the lifetime of the thread. If you want to change this feature, set M_bautodelete to False.
To make your code and MFC completely thread-safe, the CWinThread class is absolutely necessary. The thread-local data used by the framework to maintain thread-related information is managed by the CWinThread object. Any thread that uses MFC must be created by MFC because it relies on cwinthread to process thread-local data (the thread locals Storage). For example, a thread created by the Run-time function _beginthreadex cannot use any MFC APIs.
To create a thread, call the AfxBeginThread function. Depending on whether you need a worker thread or a user interface thread, there are two forms of calling AfxBeginThread. If you need a user interface thread, pass the pointer to the CRuntimeClass of your CWinThread derived class to AfxBeginThread. If you need to create a worker thread, pass the pointer to the control function and the parameters of the control function to AfxBeginThread. For worker threads and user interface threads, you can specify optional parameters to modify precedence, stack size, create flags, and security attributes.
The AfxBeginThread thread returns a pointer to the new CWinThread object.
Instead of calling AfxBeginThread, you can construct an object that CWinThread a derived class and then invoke CreateThread. This two-step construction method is useful if you need to reuse the CWinThread object between successive creation and execution of the terminating thread.
CWinThread class Members
Data members
M_bautodelete specify whether to destroy objects at the end of a thread
M_hthread the handle of the current thread
M_nthreadid the current thread ID
m_pMainWnd saves pointers to the main window of the application
m_pActiveWnd points to the main window of the container application, when an OLE server is activated on site
Constructors
CWinThread constructs a CWinThread object
CreateThread begins the execution of a CWinThread object
Operation
Getmainwnd query pointer to thread main window
GetThreadPriority gets the priority of the current thread
PostThreadMessage sends a message to another CWinThread object
ResumeThread reduce a thread's suspend count
SetThreadPriority sets the priority of the current thread
SuspendThread add a thread's suspend count
Can overload a function
ExitInstance overload for cleanup at thread termination
InitInstance overload to implement the initialization of the thread instance
OnIdle overload for thread-specific idle operations
PreTranslateMessage filter messages before messages are sent to Windows functions TranslateMessage and DispatchMessage
Isidlemessage detects a specific message
Processwndprocexception intercept thread messages and all unhandled exceptions that occur with the command handler function
Processmessagefilter intercepts messages before a particular message arrives in the application
The Run thread's control function, which has the ability to send and receive messages, can be overloaded to customize the default message loop
AfxBeginThread and CreateThread Specific differences
Specifically, the CreateThread function is the API function that Windows provides to the user and is the standard form of the SDK.
AfxBeginThread, is the compiler of the original CreateThread function encapsulation, with MFC.
And _beginthread is the Run-time library function of C.
When using AfxBeginThread, the thread function is defined as: UINT _yourthreadfun (LPVOID pparam)
When using CreateThread, the thread's function is defined as: DWORD winapi _yourthreadfun (LPVOID pparameter).
The essence of two is the same, but AfxBeginThread returns a CWinThread pointer, that is, he will be new to a CWinThread object, and this object is automatically deleted (at the end of the thread run), the inconvenience to us is not to get its state, Because it's always possible. This pointer is pointing to an already invalid memory area, so use it (if you need to understand its health) first create_suspended let him hang, then M_bautodelete=false, then ResumeThread, Finally, don't delete that pointer.
Creatthread is much more convenient, it returns a handle, if you do not use CloseHandle, you can safely understand the thread state, and finally do not closehandle,windows will release the resources (thread kernel object).
Let's take a look at the internal implementation of the AfxBeginThread function:
Copy Code code as follows:
Start worker thread
cwinthread* Afxapi AfxBeginThread (Afx_threadproc pfnthreadproc, LPVoid Pparam,
int npriority, UINT nstacksize, DWORD dwcreateflags,
Lpsecurity_attributes lpsecurityattrs)
{
ASSERT (Pfnthreadproc!= NULL);
cwinthread* PThread = debug_new CWinThread (pfnthreadproc, Pparam);
Assert_valid (PThread);
if (!pthread->createthread (dwcreateflags| Create_suspended, Nstacksize,
LPSECURITYATTRS))
{
Pthread->delete ();
return NULL;
}
VERIFY (Pthread->setthreadpriority (npriority));
if (!) ( Dwcreateflags & create_suspended))
VERIFY (Pthread->resumethread ()!= (DWORD)-1);
return pThread;
}
Start UI Thread
cwinthread* Afxapi AfxBeginThread (cruntimeclass* pthreadclass,int npriority, UINT nstacksize, DWORD dwCreateFlags, Lpsecurity_attributes lpsecurityattrs)
{
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 (Pthread->resumethread ()!= (DWORD)-1);
return pThread;
}
The main creation function is
Copy Code code as follows:
Pthread->createthread (dwcreateflags| create_suspended, Nstacksize,lpsecurityattrs))
Is
Copy Code code as follows:
。
I hope this article on the VC program for everyone to help.