class CThread
{
public:
HANDLE m_hThread;
unsigned int m_nThreadID;
public:
CThread();
DWORD ResumeThread();
DWORD SuspendThread();
int GetThreadPriority();
BOOL SetThreadPriority(int nPriority);
BOOL CreateThread(unsigned int dwCreateFlags,
UINT nStackSize,
LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL);
BOOL ThreadRunning() {return (m_iSuspendCount == 0);};
virtual BOOL KillThread();
virtual BOOL InitInstance();
virtual ~CThread();
virtual int Run() = 0;
virtual int ExitInstance() = 0;
public:
int m_iSuspendCount; //Suspend Count;
BOOL m_bExit; //线程退出标志位
HANDLE m_hEvtExit; //线程退出事件
protected:
void InitialRes();
};
#include "stdafx.h"
#include "Thread.h"
#include
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
unsigned WINAPI _ThreadEntry(LPVOID pParam)
{
CThread * pThread = (CThread *)pParam;
ASSERT_DLL(pThread != NULL);
ASSERT_DLL(pThread->m_hThread != NULL);
unsigned nResult = 0;
if (pThread->InitInstance())
{
nResult = pThread->Run();
}
nResult = pThread->ExitInstance();
pThread->m_hThread = NULL;
_endthreadex(nResult);
return nResult;
}
CThread::CThread()
{
InitialRes();
CreateThread(CREATE_SUSPENDED,0,NULL);
}
CThread::~CThread()
{
KillThread();
}
BOOL CThread::InitInstance()
{
ASSERT_DLL(this);
return (m_hEvtExit != WSA_INVALID_EVENT);
}
BOOL CThread::CreateThread(unsigned dwCreateFlags, UINT nStackSize,
LPSECURITY_ATTRIBUTES lpSecurityAttrs)
{
ASSERT_DLL(m_hThread == NULL);
LPVOID _MyThread = (LPVOID)this;
m_hThread = (HANDLE)_beginthreadex(lpSecurityAttrs,
nStackSize,
_ThreadEntry,
_MyThread,
dwCreateFlags,
&m_nThreadID);
m_iSuspendCount = (dwCreateFlags | CREATE_SUSPENDED)?1:0;
return (m_hThread)?TRUE:FALSE;
}
DWORD CThread::ResumeThread()
{
DWORD prevCount = -1;
if(m_hThread != NULL) {
prevCount = m_iSuspendCount;
if(m_iSuspendCount > 0) {
if((prevCount = ::ResumeThread(m_hThread)) != 0xFFFFFFFF)
InterlockedDecrement((LPLONG)&m_iSuspendCount);
}
}
return prevCount;
}
BOOL CThread::SetThreadPriority(int nPriority)
{
ASSERT_DLL(m_hThread != NULL);
return ::SetThreadPriority(m_hThread, nPriority);
}
int CThread::GetThreadPriority()
{
ASSERT_DLL(m_hThread != NULL);
return ::GetThreadPriority(m_hThread);
}
BOOL CThread::KillThread()
{
InterlockedExchange((LPLONG)&m_bExit,TRUE);
if(m_hThread != NULL)
{
SetEvent(m_hEvtExit);
while(m_iSuspendCount > 0) {
if(ResumeThread() == 0xFFFFFFFF)
break;
}
WaitForSingleObject(m_hThread, INFINITE);
m_hThread = NULL;
}
if(m_hEvtExit != NULL) {
CloseHandle(m_hEvtExit);
m_hEvtExit = NULL;
}
return TRUE;
}
void CThread::InitialRes()
{
m_iSuspendCount = 0;
m_hThread = NULL;
m_nThreadID = 0;
m_bExit = FALSE;
m_hEvtExit = CreateEvent(NULL,FALSE,FALSE,NULL);
}
DWORD CThread::SuspendThread()
{
ASSERT_DLL(m_hThread != NULL);
DWORD prevCount = m_iSuspendCount;
if((prevCount = ::SuspendThread(m_hThread)) != 0xFFFFFFFF)
InterlockedIncrement((LPLONG)&m_iSuspendCount);
return prevCount;
}
int CThread::Run()
{
return 0;
}
int CThread::ExitInstance()
{
return 0;
}
class CVideoThread : public CThread
{
public:
CVideoThread(CChannel *pChl);
virtual ~CVideoThread();
public:
virtual BOOL InitInstance();
virtual int Run();
virtual int ExitInstance();
.....
}
CVideoThread::~CVideoThread()
{
KillThread();
CloseHandle(m_hEvtPreview);
m_hEvtPreview = NULL;
}
BOOL CVideoThread::InitInstance()
{
return CThread::InitInstance();
}
int CVideoThread::Run()
{
while(!m_bExit)
{
DWORD dwRet = WaitForMultipleObjects(2, hWaitEvent, FALSE, INFINITE);
if(dwRet == WAIT_OBJECT_0)
{
}
else
break;
} return 0;
}
int CVideoThread::ExitInstance()
{
return CThread::ExitInstance();
}