www.pudn.com > c++或C下如何使用多线程.zip > BaseThread.cpp


// BaseThread.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "TrdDemo.h" 
#include "BaseThread.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CBaseThread 
 
HANDLE CBaseThread::m_hAnotherDead = CreateEvent(NULL, FALSE, FALSE, NULL); 
CRITICAL_SECTION CBaseThread::m_csNumLock; 
DWORD CBaseThread::m_nCounter; 
 
IMPLEMENT_DYNAMIC(CBaseThread, CWinThread) 
 
CBaseThread::CBaseThread() 
{ 
	m_nCounter = -1; 
 
	m_bAutoDelete = FALSE; 
 
	m_hEventKill = CreateEvent(NULL, TRUE, FALSE, NULL); 
	m_hEventDead = CreateEvent(NULL, TRUE, FALSE, NULL); 
} 
 
CBaseThread::~CBaseThread() 
{ 
	CloseHandle(m_hEventKill); 
	CloseHandle(m_hEventDead); 
} 
 
BOOL CBaseThread::InitInstance() 
{ 
	while (WaitForSingleObject(m_hEventKill, 0) == WAIT_TIMEOUT) 
		BeginProcess(); 
	// TODO:  perform and per-thread initialization here 
	return FALSE; 
} 
 
int CBaseThread::ExitInstance() 
{ 
	CWinThread::Delete(); 
 
	VERIFY(SetEvent(m_hEventDead)); 
	VERIFY(SetEvent(m_hAnotherDead)); 
 
	// TODO:  perform any per-thread cleanup here 
	return CWinThread::ExitInstance(); 
} 
 
BEGIN_MESSAGE_MAP(CBaseThread, CWinThread) 
	//{{AFX_MSG_MAP(CBaseThread) 
		// NOTE - the ClassWizard will add and remove mapping macros here. 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CBaseThread message handlers