240 likes | 476 Views
2. . MFC's thread implementation MFC Internals by G. Shepherd, S. WingoExecuting a class member in its own thread (C/C Users Journal, Vol.17, No. 12, p. 57)Singleton Creation the thread-safe way (C/C Users Journal, Vol.17, No. 10, p. 43)Improving performance with thread-private heaps (C/C U
E N D
1. 1 More on threads
Konstantin Bukin
CSE791 – Advanced Windows Programming
Summer 2001
2. 2 MFC’s thread implementation MFC Internals by G. Shepherd, S. Wingo
Executing a class member in its own thread (C/C++ Users Journal, Vol.17, No. 12, p. 57)
Singleton Creation the thread-safe way (C/C++ Users Journal, Vol.17, No. 10, p. 43)
Improving performance with thread-private heaps (C/C++ Users Journal, Vol.17, No. 9, p. 50)
3. 3 Thread’s thread creation
CWinThread* AFXAPI AfxBeginThread(AFX_THREADPROC pfnThreadProc, LPVOID pParam,
int nPriority, UINT nStackSize,
DWORD dwCreateFlags, LPSECURITY_ATTRIBUTES lpSecurityAttrs);
CWinThread* AFXAPI AfxBeginThread(CRuntimeClass* pThreadClass,
int nPriority, UINT nStackSize,
DWORD dwCreateFlags, LPSECURITY_ATTRIBUTES lpSecurityAttrs);
4. 4 AfxBeginThread()
CWinThread* pThread = new CWinThread(pfnThreadProc, pParam);
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;
It’s all about CWinThread and it’s CreateThread()
5. 5 CWinThread class CWinThread : public CCmdTarget {
public:
CWinThread();
CWinThread(AFX_THREADPROC pfnThreadProc, LPVOID pParam);
BOOL CreateThread(DWORD dwCreateFlags = 0, UINT nStackSize = 0,
LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL);
virtual BOOL InitInstance();
virtual int Run();
virtual BOOL PreTranslateMessage(MSG* pMsg);
virtual BOOL PumpMessage(); // low level message pump
virtual BOOL OnIdle(LONG lCount); //return TRUE if more idle processing
virtual BOOL IsIdleMessage(MSG* pMsg); //checks for special messages
private:
LPVOID m_pThreadParams; //generic parameters passed to starting function
AFX_THREADPROC m_pfnThreadProc;
HANDLE m_hThread; // this thread's HANDLE
} Back to AfxBeginThread()
6. 6 CWinThread::CreateThread()