封装线程函数

 

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 <process.h>   
//
// 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();
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值