AfxBeginThread动态创建CWinThread(或派生类)线程对象,并分配相关资源.

AfxEndThread会释放线程资源和CWinThread(或派生类)线程对象.
下面这个过程的分析,这里仅仅给出简单代码用于说明问题.
首先看一下创建线程的函数.
 
 
  
  1. CWinThread* AFXAPI AfxBeginThread(CRuntimeClass* pThreadClass, 
  2.     int nPriority, UINT nStackSize, DWORD dwCreateFlags, 
  3.     LPSECURITY_ATTRIBUTES lpSecurityAttrs) 
  4.   ...... 
  5.  
  6.     // 创建线程对象.CreateObject()函数内部有new操作符. 
  7.     CWinThread* pThread = (CWinThread*)pThreadClass->CreateObject(); 
  8.  
  9.     // 创建线程. 
  10.     pThread->m_pThreadParams = NULL; 
  11.     pThread->CreateThread(dwCreateFlags|CREATE_SUSPENDED, nStackSize, lpSecurityAttrs) 
  12.  
  13.     return pThread; 
注意红色的文字,CreateObject()函数内适用new操作符创建线程对象CWinThread或者其派生类对象.
 
下面是CreateThread函数.
 
 
  
  1. BOOL CWinThread::CreateThread(DWORD dwCreateFlags, UINT nStackSize, 
  2.     LPSECURITY_ATTRIBUTES lpSecurityAttrs) 
  3.     ... ... 
  4.     // create the thread (it may or may not start to run) 
  5.     m_hThread = (HANDLE)(ULONG_PTR)_beginthreadex(lpSecurityAttrs, nStackSize,   
  6.     &_AfxThreadEntry, &startup, dwCreateFlags | CREATE_SUSPENDED, (UINT*)&m_nThreadID); 
  7.     ... ... 
  8.     return TRUE; 
这个函数调用_beginthreadex创建线程.
注意线程回调函数_AfxThreadEntry,下面是其部分代码.
 
  
  1. UINT APIENTRY _AfxThreadEntry(void* pParam) 
  2.     _AFX_THREAD_STARTUP* pStartup = (_AFX_THREAD_STARTUP*)pParam; 
  3.  
  4.     CWinThread* pThread = pStartup->pThread; 
  5.      
  6.     ... ... 
  7.     // forced initialization of the thread 
  8.     AfxInitThread(); 
  9.  
  10.   ... ... 
  11.      
  12.     AfxEndThread(nResult); 
  13.  
  14.     return 0;   // not reached 
这个函数调用了函数AfxEndThread().从这里可以看出,若CWinThread(获取派生类)对象如果能够从线程函数正常返回,一定会调用AfxEndThread()函数. 
下面是AfxEndThread函数的代码.
 
 
  
  1. void AFXAPI AfxEndThread(UINT nExitCode, BOOL bDelete) 
  2.     ... ... 
  3.     pThread->Delete(); 
  4.  
  5.     // allow cleanup of any thread local objects 
  6.     AfxTermThread(); 
  7.  
  8.     // allow C-runtime to cleanup, and exit the thread 
  9.     _endthreadex(nExitCode); 
  10.  
  11. void CWinThread::Delete() 
  12.     // delete thread if it is auto-deleting 
  13.     if (m_bAutoDelete) 
  14.         delete this
从注释和代码可以看出,该函数用来释放资源和CWinThread对象(delete this).
 
从上面的分析可以看出, 函数调用AfxBeginThread可以动态创建CWinThread(或派生类)对象,如果能够保证程序正常从线程回调函数退出(也就是CWinThread::Run()函数),就能够保证线程对象及资源被正确释放.