#ifndef THREAD_H
#define THREAD_H
#include <stdio.h>//printf
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#define THREAD_API DWORD WINAPI
#else
#include <pthread.h>
#define THREAD_API void*
#endif
class CThread

{
private:
#ifdef WIN32
HANDLE m_hThreadHandle;
DWORD m_dwThreadId;
#else
pthread_t m_dwThreadId;
#endif
bool m_bStopFlag;//是否已經“取消”該線程
bool m_bStarted;//線程已經啟動
private:
struct CThreadArg 
{
CThread* m_pThread;//保存this指針
void* m_pArg;//保存start成員函數(shù)里面的arg參數(shù)指針
};
CThreadArg* m_pThrArg;
public:
CThread();
~CThread();
int Start(void* arg = NULL);
int Wait();//成功返回0;失敗-1
void Terminate(void);//強制退出
void SetStopFlag(); //協(xié)作性退出
bool GetStopFlag(); 

unsigned int GetID(void);
protected:
void SetThreadArg(CThreadArg* pThrArg = NULL);
CThreadArg* GetThreadArg(void);
virtual void Run(void* arg) = 0;//1.子類重載此函數(shù),在run里面完成所有功能.2.子類要檢查GetStopFlag(),以配合對該線程的“停止”;或者采用強制終止Terminate()
private:
static THREAD_API Routine(void* arg);
};

#endif



下面是Thread.cpp

#include "Thread.h"
CThread::CThread():m_dwThreadId(0),m_bStopFlag(false),m_bStarted(false)

{
#ifdef WIN32
m_hThreadHandle = NULL;
SetThreadArg(NULL);
#endif
}
CThread::~CThread()

{
#ifdef WIN32
if (m_hThreadHandle != NULL)
{
CloseHandle(m_hThreadHandle);
}
#endif
if (m_pThrArg != NULL)
{
delete m_pThrArg;
m_pThrArg = NULL;
}
}

int CThread::Start(void* arg /**//* = NULL */)

{
if (m_bStarted)
{
return 0;
}
else
{
m_bStarted = true;
}
CThreadArg *pThrArg = new CThreadArg();
pThrArg->m_pArg = arg;
pThrArg->m_pThread = this;

#ifdef WIN32
m_hThreadHandle = CreateThread(NULL,NULL,Routine,(void*)pThrArg,NULL,&m_dwThreadId);
if (m_hThreadHandle != NULL)
{
m_pThrArg = pThrArg;
return 0;
}
else
{
delete pThrArg;
return -1;
}
#else
int ret = pthread_create(&m_dwThreadId,NULL,Routine,(void*)pThrArg);
if (ret == 0)
{
m_pThrArg = pThrArg;
return 0;
}
else
{
delete pThrArg;
return -1;
}
#endif
}
int CThread::Wait()

{
#ifdef WIN32
::WaitForSingleObject(m_hThreadHandle,INFINITE);
BOOL bRet = CloseHandle(m_hThreadHandle);
if (bRet)
{
m_hThreadHandle = NULL;
return 0;
}
else
return -1;
#else
int nRet = pthread_join(m_dwThreadId,NULL);
if(nRet == 0)
return 0;
else
return -1;
#endif
}
void CThread::SetStopFlag()

{
m_bStopFlag = true;
return;
}
bool CThread::GetStopFlag()

{
return m_bStopFlag;
}
void CThread::Terminate()

{
#ifdef WIN32
if (GetCurrentThreadId() == m_dwThreadId)
{
ExitThread(0);
return;
}
else
{
TerminateThread(m_hThreadHandle,0);
DWORD dwRet = 0;
GetExitCodeThread(m_hThreadHandle,&dwRet);
while (dwRet == STILL_ACTIVE)
{
printf("still active \n");
Sleep(100);
TerminateThread(m_hThreadHandle,0);
GetExitCodeThread(m_hThreadHandle,&dwRet);
}
}
#else
if (pthread_self() == m_dwThreadId)
{
pthread_exit(NULL);
return;
}
else
{
pthread_cancel(m_dwThreadId);
}
#endif
SetStopFlag();
return;
}
unsigned int CThread::GetID(void)

{
return (unsigned int) m_dwThreadId;
}

void CThread::SetThreadArg(CThreadArg* pThrArg /**//* = NULL */)

{
m_pThrArg = pThrArg;
return;
}
CThread::CThreadArg* CThread::GetThreadArg()

{
return m_pThrArg;
}
THREAD_API CThread::Routine(void* arg)

{
CThreadArg* pThrArg = (CThreadArg*)arg;
#ifndef WIN32
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
#endif
pThrArg->m_pThread->Run(pThrArg->m_pArg);
return NULL;
}
自己用的,如果你有好的想法,告訴我!

