线程包装类Word下载.docx
《线程包装类Word下载.docx》由会员分享,可在线阅读,更多相关《线程包装类Word下载.docx(19页珍藏版)》请在冰豆网上搜索。
![线程包装类Word下载.docx](https://file1.bdocx.com/fileroot1/2023-2/6/526fcae1-dd9c-4b55-ac77-688d66338845/526fcae1-dd9c-4b55-ac77-688d663388451.gif)
//线程池满
JADE_THREAD_ERROR_STOP
0x00000002
//该线程以停止
JADE_THREAD_ERROR_USED
0x00000003
//指定的线程被占用
JADE_LOCAL_MODEL
0x00000010
//
JADE_THREAD_CREATED
0x00000004
//线程已创建
JADE_THREAD_FAIL
0x00000005
//失败
JADE_THREAD_FALSE
0x00000006
//异常错误
JADE_THREAD_KILLED
0x00000007
//已删除
JADE_ALLOC_LOCAL
0x00000008
//TLS内存处理方式
JADE_ALLOC_GLOBAL
0x00000009
//全局内存分配方式
JADE_FALSE
//操作错误
JADE_MANAGE_FILO
JADE_SUCCEEDED(dwStatus)
((DWORD)(dwStatus)==0)
/*----------------------------------------------------------------------------------
功能说明:
任务处理基类使用者,请从该类进行子类化的派生。
每个抽象类必须实现TaskProc方法以便Jade_Thread
Pool调用您所需要的处理过程。
方法说明:
TaskProc
处理过程
OtherProc
保留处理过程
参数:
LPVOID
pParam
目前为该任务类的this指针
DWORD
dwID
该任务类所占用的线程ID
-----------------------------------------------------------------------------------*/
class
Jade_TaskBase
{
public
Jade_TaskBase(void)
{};
virtual
~Jade_TaskBase(void)
public:
TaskProc(LPVOID
pParam,DWORD
dwID)
=
0;
OtherProc(LPVOID
dwOther)
};
JADE_THREAD_H
windows.h>
"
Jade_config_Thread_pool.h"
/*----------------------------------------------------------------------------------
功能描述:
一个线程的抽象表现,实现:
运行、停止、重起、建立,销毁、
-----------------------------------------------------------------------------------*/
Jade_ThreadByEvent
{
Jade_ThreadByEvent();
~Jade_ThreadByEvent();
enum
ThreadStatus{TLS_RUN
1,TLS_STOP,TLS_KILL};
Create(DWORD
dwStackSize,DWORD
dwReturn
0,DWORD
dwLevel
0,Jade_TaskBase
*pTask
NULL);
void
SetTimeOut(DWORD
dwTime
INFINITE);
//是否需要?
GetThreadRunTime(double
&
dwTime);
//取得线程运行时间
Run(Jade_TaskBase
*pTask);
Stop();
Restart();
Kill();
CheckState(DWORD
dwState);
ResetTask(Jade_TaskBase
SetPriority(DWORD
dwLevel);
private:
static
CALLBACK
ThreadProc(LPVOID
lpParam);
FilterCode(DWORD
dwErrCode,LPVOID
lpVoid);
Abend(DWORD
dwCode
0);
HANDLE
m_hThread;
//线程的实现描述
m_hEvent[3];
//2-运行、1-停止、0-结束(线程状态)
m_hWaitEvent[2];
//0-停止、1-结束(等待返回确认状态)
*m_pTask;
//所要执行的任务
m_dwReturn;
//该线程的返回值
m_dwID;
//线程ID
m_dwState;
//线程状态
m_dwTimeOut;
//线程执行超时
m_dwLevel;
//运行级别
defined(__JADE_THREADPOOL_INLINE__)
Jade_ThreadByEvent.inl"
/*__JADE_THREAD_INLINE__*/
#endif
stdafx.h"
time.h>
Jade_ThreadByEvent.h"
!
Jade_ThreadByEvent:
Jade_ThreadByEvent():
m_hThread(NULL),m_pTask(NULL),m_dwReturn(0),m_dwID(0)
m_dwState(0),m_dwTimeOut(INFINITE)
m_hEvent[0]
CreateEvent(NULL,TRUE,FALSE,"
Local\\Thread_Run"
);
m_hEvent[1]
Local\\Thread_Stop"
m_hEvent[2]
Local\\Thread_Kill"
m_hWaitEvent[0]
Local\\Return
Stop"
m_hWaitEvent[1]
Kill"
}
~Jade_ThreadByEvent()
//Kill
Thread
Event
for(int
i=0;
i<
3;
i++)
if(m_hEvent[i]!
=NULL)
CloseHandle(m_hEvent[i]);
m_hEvent[i]
NULL;
CloseHandle(m_hWaitEvent[0]);
CloseHandle(m_hWaitEvent[1]);
ZeroMemory(m_hWaitEvent,sizeof(HANDLE)*2);
Stop()
if(JADE_SUCCEEDED(CheckState(TLS_STOP)))
return
JADE_OK;
SetEvent(m_hEvent[1]);
/*---------------------------------------------------------------------------------
方法说明:
杀死线程
---------------------------------------------------------------------------------*/
Kill()
dwKill
0,dwExitTime
200;
if(m_hThread
NULL)
//检查线程状态
SetEvent(m_hEvent[0]);
WaitForSingleObject(m_hWaitEvent[1],INFINITE);
do
if(!
GetExitCodeThread(m_hThread,&
dwKill)||dwExitTime==1000)
Abend();
//异常非法结束
GetLastError();
Sleep(dwExitTime);
dwExitTime++;
}while(dwKill
m_dwReturn);
CloseHandle(m_hThread);
m_hThread
JADE_THREAD_KILLED;
/*---------------------------------------------------------------------------------------------
方法功能:
建立线程,设定执行任务
Input:
dwStackSize
堆
返回值
lpParam
任务的参数
JADE_TASKBASE*
任务
返回值:
执行结果
----------------------------------------------------------------------------------------------*/
dwReturn,DWORD
dwLevel,Jade_TaskBase
*pTask)
NULL||m_dwID
0)
JADE_THREAD_CREATED;
__try
if(pTask
CreateThread(NULL,dwStackSize,ThreadProc,this,0,&
m_dwID);
m_pTask
pTask;
m_dwState
TLS_RUN;
else
CreateThread(NULL,dwStackSize,ThreadProc,this,CREATE_SUSPENDED,&
TLS_STOP;
if(dwLevel
0
m_hThread!
SetThreadPriority(m_hThread,dwLevel))
OutputDebugString("
The
Priority
Failed/r/n"
}
m_dwReturn
dwReturn;
__except(FilterCode(GetExceptionCode(),this))
printf("
线程创建错误\r\n"
JADE_THREAD_FAIL;
方法说名:
开始运行
input:
执行的任务
返回值:
------------------------------------------------------------------------------------*/
switch(m_dwState)
case
TLS_RUN:
SetEvent(m_hEvent[2]);
break;
TLS_STOP:
/*-------------------------------------------------------------------------------------
重新启动线程
注:
将停止状态的线程置运行状态
--------------------------------------------------------------------------------------*/
Restart()
0,dw
ResetEvent(m_hEvent[1]);
ResumeThread(m_hThread);
if(dwReturn
==
-1)
for(;
>
)
/*---------------------------------------------------------------------------------------
重新设置线程的任务
任务体
----------------------------------------------------------------------------------------*/
char
chCode[64]
dwResult
sprintf(chCode,"
Except
Code
is
%d"
dwResult);
OutputDebugString(chCode);
JADE_THREAD_FALSE;
dwTime)
m_dwTimeOut
dwTime;
/*--------------------------------------------------------------------------------------
线程实现体
Jade_Thread指针
WaitForMultipleObjects有检查顺序所以运行状态要靠后,结束等待状态设置比较靠前
---------------------------------------------------------------------------------------*/
lpParam)
Jade_ThreadByEvent
*pThis
static_cast<
Jade_ThreadByEvent*>
(lpParam);
pThis->
hEvent[3]
memcpy(hEvent,pThis->
m_hEvent,sizeof(hEvent));
dwState
int
nOther
clock_t
tStart
=0,tFinish
LOOP:
WaitForMultipleObjects(3,hEvent,FALSE,pThis->
m_dwTimeOut);
switch(dwState)
WAIT_OBJECT_0
+
2:
//Run
m_pTask->
TaskProc(pThis->
m_pTask,pThis->
1:
//Stop
SuspendThread(pThis->
m_hThread);
//由于是线程内停止所以因该只能被执行一次
0:
//Kill
TLS_KILL;
SetEvent(pThis->
m_hWaitEvent[1])?
m_dwReturn:
WAIT_FAILED: