上篇文章寫了如何實現線程類的創建及原理。這篇文章實現其基類_CAsThread。
_CAsThread::_CAsThread()
{
m_bExit = false;
m_bStart = false;
m_hThread = 0;
memcpy(m_strName, "AsThread", strlen("AsThread")+1);
m_pExceptFun= NULL;
}
//---------------------------------------------------------------------------
_CAsThread::~_CAsThread()
{
Stop();
}
#define MS_VC_EXCEPTION 0x406d1388
typedef struct tagTHREADNAME_INFO
{
DWORD dwType; // must be 0x1000
LPCSTR szName; // pointer to name (in same addr space)
DWORD dwThreadID; // thread ID (-1 caller thread)
DWORD dwFlags; // reserved for future use, most be zero
} THREADNAME_INFO;
void SetThreadName(DWORD dwThreadID, LPCTSTR szThreadName)
{
THREADNAME_INFO info;
info.dwType = 0x1000;
info.szName = szThreadName;
info.dwThreadID = dwThreadID;
info.dwFlags = 0;
__try
{
RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(DWORD), (DWORD *)&info);
}
__except(EXCEPTION_CONTINUE_EXECUTION)
{
}
}
//---------------------------------------------------------------------------
bool _CAsThread::Start(void)
{
if(true == m_bStart)
return true;
if(0 == m_hThread)
{
unsigned threadID;
m_hThread = (HANDLE)_beginthreadex(NULL, 0, &WorkItem, (void*)this, 0, &threadID);
int tHandle = (int)m_hThread;
if(0 == tHandle || -1 == tHandle)
return false;
SetThreadName(threadID, m_strName);
}
char tDebugBuf[250] = {0};
#ifndef __BORLANDC__
//輸出調試信息
sprintf_s(tDebugBuf,"------線程%s開始運行------\n" ,m_strName);
#else
sprintf(tDebugBuf,"------線程%s開始運行------\n" ,m_strName);
#endif
OutputDebugString(tDebugBuf);
m_bExit = false;
m_bStart = true;
return true;
}
//---------------------------------------------------------------------------
void _CAsThread::PrepareStop(void)
{
m_bExit = true;
}
//---------------------------------------------------------------------------
bool _CAsThread::Stop(void)
{
//設置程序退出標志
m_bStart = false;
m_bExit = true;
//等待線程退出
int tHandle = (int)m_hThread;
if(0 != tHandle && -1 != tHandle)
{
WaitForSingleObject(m_hThread,INFINITE);
CloseHandle(m_hThread);
char tDebugBuf[250] = {0};
#ifndef __BORLANDC__
//輸出調試信息
sprintf_s(tDebugBuf,"------線程%s句柄關閉------\n" ,m_strName);
#else
sprintf(tDebugBuf,"------線程%s句柄關閉------\n" ,m_strName);
#endif
OutputDebugString(tDebugBuf);
}
m_hThread = 0;
return true;
}
//---------------------------------------------------------------------------
void _CAsThread::SetName(const char* vName)
{
if(NULL == vName)
return;
if(100 < strlen(vName))
memcpy(m_strName, vName, strlen(vName)+1);
else
memcpy(m_strName, vName, 99);
}
//---------------------------------------------------------------------------
void _CAsThread::SetTranslator(void)
{
if(NULL == m_pExceptFun)
return;
SetErrorMode(SEM_FAILCRITICALERRORS|SEM_NOGPFAULTERRORBOX);
_set_se_translator(m_pExceptFun);
}
unsigned __stdcall _CAsThread::WorkItem(void* vContext)
{
_CAsThread* pTimer = (_CAsThread*)vContext;
pTimer->SetTranslator();
while(1)
{
if(true == pTimer->m_bExit)
break;
pTimer->WorkThread();
}
char tDebugBuf[250] = {0};
#ifndef __BORLANDC__
//輸出調試信息
sprintf_s(tDebugBuf,"------線程%s開始退出------\n" ,pTimer->m_strName);
#else
sprintf(tDebugBuf,"------線程%s開始退出------\n" , pTimer->m_strName);
#endif
OutputDebugString(tDebugBuf);
return 0;
}
本文出自 “阿木雪” 博客,謝絕轉載!