The previous article describes how to create a Thread class and how it works. This article implements its base class _ CAsThread.
_ CAsThread: _ CAsThread ()
{
M_bExit = false;
M_bStart = false;
M_hThread = 0;
Memcpy (m_strName, "AsThread", strlen ("AsThread") + 1 );
M_p;tfun = 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 );
}
_ Execut (prediction_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 [1, 250] = {0 };
# Ifndef _ BORLANDC __
// Output debugging information
Sprintf_s (tDebugBuf, "------ thread % s starts running ------ \ n", m_strName );
# Else
Sprintf (tDebugBuf, "------ thread % s start running ------ \ 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)
{
// Set the exit flag of the program
M_bStart = false;
M_bExit = true;
// Wait for the thread to exit
Int tHandle = (int) m_hThread;
If (0! = THandle &-1! = THandle)
{
WaitForSingleObject (m_hThread, INFINITE );
CloseHandle (m_hThread );
Char tDebugBuf [1, 250] = {0 };
# Ifndef _ BORLANDC __
// Output debugging information
Sprintf_s (tDebugBuf, "------ thread % s handle closed ------ \ n", m_strName );
# Else
Sprintf (tDebugBuf, "------ thread % s handle closed ------ \ 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_pw.tfun)
Return;
SetErrorMode (SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX );
_ Set_se_translator (m_pw.tfun );
}
Unsigned _ stdcall _ CAsThread: WorkItem (void * vContext)
{
_ CAsThread * pTimer = (_ CAsThread *) vContext;
PTimer-> SetTranslator ();
While (1)
{
If (true = pTimer-> m_bExit)
Break;
PTimer-> WorkThread ();
}
Char tDebugBuf [1, 250] = {0 };
# Ifndef _ BORLANDC __
// Output debugging information
Sprintf_s (tDebugBuf, "------ thread % s start to exit ------ \ n", pTimer-> m_strName );
# Else
Sprintf (tDebugBuf, "------ thread % s start to exit ------ \ n", pTimer-> m_strName );
# Endif
OutputDebugString (tDebugBuf );
Return 0;
}
This article is from the "Amu Xue" blog and will not be reproduced!