一、使用方法:
1. 使用独立的线程类管理线程
2. 使用派生类重载处理过程函数Procedure()
3. 主程序中使用:
//创建并开始
CThreadManager* m_MyDerivedThreadManager;
m_MyDerivedThreadManager = new CMyDerivedThreadManager();
m_MyDerivedThreadManager->Create();
m_MyDerivedThreadManager->Start();
//结束并销毁
m_MyDerivedThreadManager->Stop();
delete m_MyDerivedThreadManager;
二、ThreadManager.h
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#pragma
once
/*
线程管理类
(1) 可以直接使用此类,并指定外部处理函数
(2) 可以派生类,重载Procedure(),在派生类中处理事务
*/
typedef long ( * PFUN)( void * _param); // 外部定义的线程处理函数指针,参数用void*表示
typedef vector < HANDLE > HandleArr;
typedef vector < int > intArr;
const int THREADMANAGER_USERLESS = (unsigned int ) - 1 ;
class CThreadManager
{
public :
CThreadManager( void );
virtual ~ CThreadManager( void );
CThreadManager(PFUN _pFun, void * _pParam);
void Create(); // 开启线程
void Start(); // 开始工作
void Stop(); // 关闭线程
virtual int Procedure(); // 线程处理,派生类中重写此函数(函数中不需要无限循环,否则将不能停止线程)
protected :
int m_nWaitTime;
int m_nThreadNo;
PFUN m_pFn; // 外部函数指针
void * m_pParam; // 参数
HANDLE m_eventArr[ 2 ]; // 2个等待的事件
HANDLE m_eventExit; // 是否退出
HANDLE m_eventWorked; // 是否工作
void InitParam();
static DWORD WINAPI WorkProcess( void * _param);
static DWORD WINAPI WorkProcessEx( void * _param);
};
/*
线程管理类
(1) 可以直接使用此类,并指定外部处理函数
(2) 可以派生类,重载Procedure(),在派生类中处理事务
*/
typedef long ( * PFUN)( void * _param); // 外部定义的线程处理函数指针,参数用void*表示
typedef vector < HANDLE > HandleArr;
typedef vector < int > intArr;
const int THREADMANAGER_USERLESS = (unsigned int ) - 1 ;
class CThreadManager
{
public :
CThreadManager( void );
virtual ~ CThreadManager( void );
CThreadManager(PFUN _pFun, void * _pParam);
void Create(); // 开启线程
void Start(); // 开始工作
void Stop(); // 关闭线程
virtual int Procedure(); // 线程处理,派生类中重写此函数(函数中不需要无限循环,否则将不能停止线程)
protected :
int m_nWaitTime;
int m_nThreadNo;
PFUN m_pFn; // 外部函数指针
void * m_pParam; // 参数
HANDLE m_eventArr[ 2 ]; // 2个等待的事件
HANDLE m_eventExit; // 是否退出
HANDLE m_eventWorked; // 是否工作
void InitParam();
static DWORD WINAPI WorkProcess( void * _param);
static DWORD WINAPI WorkProcessEx( void * _param);
};
三、ThreadManager.cpp
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include
"
StdAfx.h
"
#include " ThreadManager.h "
CThreadManager::CThreadManager( void )
{
m_pFn = NULL;
m_pParam = NULL;
InitParam();
}
CThreadManager::CThreadManager(PFUN _pFun, void * _pParam)
{
m_pFn = _pFun;
m_pParam = _pParam;
m_nWaitTime = THREADMANAGER_USERLESS;
InitParam();
}
CThreadManager:: ~ CThreadManager( void )
{
Stop();
CloseHandle(m_eventArr[ 0 ]);
CloseHandle(m_eventArr[ 1 ]);
CloseHandle(m_eventExit);
CloseHandle(m_eventWorked);
}
int CThreadManager::Procedure()
{
return 0 ;
}
void CThreadManager::InitParam()
{
m_nWaitTime = THREADMANAGER_USERLESS;
m_eventArr[ 0 ] = ::CreateEvent(NULL,FALSE,FALSE,NULL);
m_eventArr[ 1 ] = ::CreateEvent(NULL,FALSE,FALSE,NULL);
m_eventExit = ::CreateEvent(NULL,TRUE,FALSE,NULL);
m_eventWorked = ::CreateEvent(NULL,FALSE,FALSE,NULL);
}
void CThreadManager::Create()
{
DWORD dwID;
HANDLE hTemp;
if (m_pFn == NULL)
{
hTemp = ::CreateThread(NULL, 0 ,WorkProcessEx, this ,NULL, & dwID);
}
else
{
hTemp = ::CreateThread(NULL, 0 ,WorkProcess, this ,NULL, & dwID);
}
::CloseHandle(hTemp);
}
void CThreadManager::Start()
{
::SetEvent(m_eventArr[ 1 ]);
::WaitForSingleObject(m_eventWorked,INFINITE);
}
void CThreadManager::Stop()
{
::SetEvent(m_eventArr[ 0 ]);
::WaitForSingleObject(m_eventExit,INFINITE);
}
DWORD CThreadManager::WorkProcess( void * _param)
{
CThreadManager * pObj = (CThreadManager * )_param;
DWORD dwRet =- 1 ;
while ( 1 )
{
dwRet = ::WaitForMultipleObjects(
2 , // nCount
pObj -> m_eventArr, // lpHandles:
FALSE, // bWaitAll : 等待多个内核对象
INFINITE);
if (dwRet == WAIT_TIMEOUT)
continue ;
dwRet -= WAIT_OBJECT_0;
if (dwRet == 0 )
goto exit;
else
{
::SetEvent(pObj -> m_eventWorked);
if (( * pObj -> m_pFn)(pObj -> m_pParam) ==- 1 )
{
goto exit;
}
}
}
exit:
::SetEvent(pObj -> m_eventExit);
return 0 ;
}
DWORD CThreadManager::WorkProcessEx( void * _param)
{
CThreadManager * pObj = (CThreadManager * )_param;
DWORD dwRet =- 1 ;
while ( 1 )
{
dwRet = ::WaitForMultipleObjects(
2 ,
pObj -> m_eventArr,
FALSE,
INFINITE);
if (dwRet == WAIT_TIMEOUT)
continue ;
dwRet -= WAIT_OBJECT_0;
if (dwRet == 0 )
goto exit1;
else
{
::SetEvent(pObj -> m_eventWorked);
if (pObj -> Procedure() ==- 1 )
{
goto exit1;
}
}
}
exit1:
::SetEvent(pObj -> m_eventExit);
return 0 ;
}
// /
// 注:WaitForMultipleObjects返回值减WAIT_OBJECT_0 就是参数lpHandles数组的序号。如果同时有多个内核对象被触发,则返回 只是其中序号最小的那个.
#include " ThreadManager.h "
CThreadManager::CThreadManager( void )
{
m_pFn = NULL;
m_pParam = NULL;
InitParam();
}
CThreadManager::CThreadManager(PFUN _pFun, void * _pParam)
{
m_pFn = _pFun;
m_pParam = _pParam;
m_nWaitTime = THREADMANAGER_USERLESS;
InitParam();
}
CThreadManager:: ~ CThreadManager( void )
{
Stop();
CloseHandle(m_eventArr[ 0 ]);
CloseHandle(m_eventArr[ 1 ]);
CloseHandle(m_eventExit);
CloseHandle(m_eventWorked);
}
int CThreadManager::Procedure()
{
return 0 ;
}
void CThreadManager::InitParam()
{
m_nWaitTime = THREADMANAGER_USERLESS;
m_eventArr[ 0 ] = ::CreateEvent(NULL,FALSE,FALSE,NULL);
m_eventArr[ 1 ] = ::CreateEvent(NULL,FALSE,FALSE,NULL);
m_eventExit = ::CreateEvent(NULL,TRUE,FALSE,NULL);
m_eventWorked = ::CreateEvent(NULL,FALSE,FALSE,NULL);
}
void CThreadManager::Create()
{
DWORD dwID;
HANDLE hTemp;
if (m_pFn == NULL)
{
hTemp = ::CreateThread(NULL, 0 ,WorkProcessEx, this ,NULL, & dwID);
}
else
{
hTemp = ::CreateThread(NULL, 0 ,WorkProcess, this ,NULL, & dwID);
}
::CloseHandle(hTemp);
}
void CThreadManager::Start()
{
::SetEvent(m_eventArr[ 1 ]);
::WaitForSingleObject(m_eventWorked,INFINITE);
}
void CThreadManager::Stop()
{
::SetEvent(m_eventArr[ 0 ]);
::WaitForSingleObject(m_eventExit,INFINITE);
}
DWORD CThreadManager::WorkProcess( void * _param)
{
CThreadManager * pObj = (CThreadManager * )_param;
DWORD dwRet =- 1 ;
while ( 1 )
{
dwRet = ::WaitForMultipleObjects(
2 , // nCount
pObj -> m_eventArr, // lpHandles:
FALSE, // bWaitAll : 等待多个内核对象
INFINITE);
if (dwRet == WAIT_TIMEOUT)
continue ;
dwRet -= WAIT_OBJECT_0;
if (dwRet == 0 )
goto exit;
else
{
::SetEvent(pObj -> m_eventWorked);
if (( * pObj -> m_pFn)(pObj -> m_pParam) ==- 1 )
{
goto exit;
}
}
}
exit:
::SetEvent(pObj -> m_eventExit);
return 0 ;
}
DWORD CThreadManager::WorkProcessEx( void * _param)
{
CThreadManager * pObj = (CThreadManager * )_param;
DWORD dwRet =- 1 ;
while ( 1 )
{
dwRet = ::WaitForMultipleObjects(
2 ,
pObj -> m_eventArr,
FALSE,
INFINITE);
if (dwRet == WAIT_TIMEOUT)
continue ;
dwRet -= WAIT_OBJECT_0;
if (dwRet == 0 )
goto exit1;
else
{
::SetEvent(pObj -> m_eventWorked);
if (pObj -> Procedure() ==- 1 )
{
goto exit1;
}
}
}
exit1:
::SetEvent(pObj -> m_eventExit);
return 0 ;
}
// /
// 注:WaitForMultipleObjects返回值减WAIT_OBJECT_0 就是参数lpHandles数组的序号。如果同时有多个内核对象被触发,则返回 只是其中序号最小的那个.