一个线程封装类

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> class CThread
{
public:
/**//**
*DefaultConstructor
*/

CThread()
{
m_pThreadFunction
=CThread::EntryPoint;
m_runthread
=FALSE;
}


/**//**
*DefaultDestructor
*alsodestroysthethread
*/

~CThread()
{
if(m_hThread)
Stop(
true);//threadstillrunning,soforcethethreadtostop!
}

/**//**
*Startsthethread.
*@paramdwCreationFlagstheflagstouseforcreatingthethread.seeCreateThread()inthewindowssdk.
*/

DWORDStart(DWORDdwCreationFlags
=0)
{
m_runthread
=true;
m_hThread
=CreateThread(NULL,0,m_pThreadFunction,this,0,&dwCreationFlags);
m_dwExitCode
=(DWORD)-1;

returnGetLastError();
}


/**//**
*Stopsthethread.
*
*@parambForceKilliftrue,theThreadiskilledimmediately
*/

DWORDStop(
boolbForceKill=false)
{
if(m_hThread)
{
//尝试"温柔地"结束线程
if(m_runthread==TRUE)
m_runthread
=FALSE;//first,trytostopthethreadnice

GetExitCodeThread(m_hThread,
&m_dwExitCode);

if(m_dwExitCode==STILL_ACTIVE&&bForceKill)
{//强制杀死线程
TerminateThread(m_hThread,DWORD(-1));
m_hThread
=NULL;
}

}


returnm_dwExitCode;
}

/**//**
*Stopsthethread.firsttellthethreadtostopitselfandwaitforthethreadtostopitself.
*iftimeoutoccursandthethreadhasn'tstoppedyet,thenthethreadiskilled.
*@paramtimeoutmillisecondstowaitforthethreadtostopitself
*/

DWORDStop(WORDtimeout)
{
Stop(
false);
WaitForSingleObject(m_hThread,timeout);
//等待一段时间
returnStop(true);
}


/**//**
*suspendsthethread.i.e.thethreadishaltedbutnotkilled.TostartasuspendedthreadcallResume().
*/

DWORDSuspend()
{//挂起线程
returnSuspendThread(m_hThread);
}


/**//**
*resumesthethread.thismethodstartsacreatedandsuspendedthreadagain.
*/

DWORDResume()
{//恢复线程
returnResumeThread(m_hThread);
}


/**//**
*setsthepriorityofthethread.
*@paramprioritythepriority.seeSetThreadPriority()inwindowssdkforpossiblevalues.
*@returntrueifsuccessful
*/

BOOLSetPriority(
intpriority)
{//设置线程优先级
returnSetThreadPriority(m_hThread,priority);
}


/**//**
*getsthecurrentpriorityvalueofthethread.
*@returnthecurrentpriorityvalue
*/

intGetPriority()
{//获取线程优先级
returnGetThreadPriority(m_hThread);
}


protected:

/**//**
*子类应该重写此方法,这个方法是实际的工作线程函数
*/

virtualDWORDThreadMethod()=0;

private:

/**//**
*DONToverridethismethod.
*
*thismethodisthe"function"usedwhencreatingthethread.itisstaticsothatway
*apointertoitisavailableinsidetheclass.thismethodcallsthenthevirtual
*methodoftheparentclass.
*/

staticDWORDWINAPIEntryPoint(LPVOIDpArg)
{
CThread
*pParent=reinterpret_cast<CThread*>(pArg);
pParent
->ThreadMethod();//多态性,调用子类的实际工作函数
return0;
}


private:
HANDLEm_hThread;
/**////<ThreadHandle线程句柄
DWORDm_dwTID;///<ThreadID线程ID
LPVOIDm_pParent;///<thispointeroftheparentCThreadobject
DWORDm_dwExitCode;///<ExitCodeofthethread线程退出码

protected:
LPTHREAD_START_ROUTINEm_pThreadFunction;
/**////<工作线程指针
BOOLm_runthread;///<线程是否继续运行的标志
};


子类只需要从此类继承并重写ThreadMethod()方法就可以了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值