📄 thdpool.cpp
字号:
// ThdPool.cpp: implementation of the CThdPool class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "MDF.h"
#include "ThdPool.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
extern CMDFApp theApp ;
//##ModelId=44B6F8830233
CThdPool::CThdPool()
{
sCommItem InitItem ;
InitItem.pEQ = NULL ; //设备指针
//InitItem.pThread = NULL ; //工作线程指针
InitItem.pSock = NULL ;
InitItem.unState = 3 ; //空闲通讯线程
InitItem.strBuf.Empty() ; //清空缓存区
m_ThdArray.RemoveAll() ;
//建立 500 空间的线程池
for(int i = 0 ; i <= 500 ; i++)
{
InitItem.unID = i ; //通讯线ID
m_ThdArray.Add(InitItem) ;
}
//hEvent = CreateEvent ( NULL , FALSE , TRUE , NULL ) ; //建立信号状态
m_CurrThreadCount = 0 ; //最大并发线程为 50
}
//##ModelId=44B6F8830234
CThdPool::~CThdPool()
{
m_ThdArray.RemoveAll() ;
}
//DEL int CThdPool::AddCommItem(sCommEQ* pEQTemp , sCommPack& InPack , CString strBuf , CString strSource)
//DEL {
//DEL int i,nRet,iMax ;
//DEL
//DEL iMax = m_ThdArray.GetSize() ;
//DEL
//DEL for( i = 0 ; i < iMax ; i++ )
//DEL {
//DEL //-问题-存在清除处理线空间问题。
//DEL if( m_ThdArray[i].unState == 3 ) // 空闲线程
//DEL {
//DEL m_ThdArray[i].unState = 0 ; // 0为未处理
//DEL m_ThdArray[i].unID = i ;
//DEL m_ThdArray[i].pEQ = pEQTemp ; //设备指针
//DEL //m_ThdArray[i].pPack = new sCommPack () ; //协议包指针
//DEL //复制通讯数据包
//DEL //*(m_ThdArray[i].pPack) = InPack ;
//DEL m_ThdArray[i].strBuf = strBuf ; //保存收到数据
//DEL m_ThdArray[i].strSource = strSource ; //保存收到数据来源
//DEL
//DEL if(m_CurrThreadCount <= 500 ) //并发线程不能超过500
//DEL {
//DEL m_ThdArray[i].unState = 1 ; //正在处理
//DEL //启动COMM的通讯线程
//DEL m_ThdArray[i].pThread = theApp.m_pComm->CreateCommThd((LPVOID)&(m_ThdArray[i])) ;
//DEL m_CurrThreadCount++ ;
//DEL }
//DEL return 0 ;
//DEL }
//DEL }
//DEL return -1 ;
//DEL }
//##ModelId=44B6F8830228
void CThdPool::RemoveItem(int nIndex)
{
//WaitForSingleObject ( hEvent , INFINITE ) ;
m_ThdArray[nIndex].pEQ = NULL ;
//m_ThdArray[nIndex].pThread = NULL ;
m_ThdArray[nIndex].pSock = NULL ;
m_ThdArray[nIndex].strBuf.Empty() ;
m_ThdArray[nIndex].strSource.Empty() ;
m_ThdArray[nIndex].unState = 3 ; //置通讯线程置为空闲
memset(m_ThdArray[nIndex].ucBuf,0,1024) ;
m_CurrThreadCount-- ;
//SetEvent ( hEvent ) ;
}
//##ModelId=44B6F8830227
int CThdPool::GetCurThdCount()
{
return m_CurrThreadCount ;
}
//##ModelId=44B6F883021A
BOOL CThdPool::AddCommItem(UCHAR *buf, int len , CString strSource,int nType,LPVOID pSock)
{
CString strTip ;
int i,iMax ;
iMax = m_ThdArray.GetSize() ;
for( i = 0 ; i < iMax ; i++ )
{
//-问题-存在清除处理线空间问题。
if( m_ThdArray[i].unState == 3 ) // 空闲线程
{
m_ThdArray[i].unID = i ; //通讯线程ID
m_ThdArray[i].unState = 0 ; //未处理
m_ThdArray[i].pEQ = NULL ; //设备指针
memset(&(m_ThdArray[i].InPack),0,sizeof(m_ThdArray[i].InPack)) ;
memset(m_ThdArray[i].ucBuf,0,sizeof(m_ThdArray[i].ucBuf)) ;
memcpy(m_ThdArray[i].ucBuf,buf,sizeof(UCHAR)*len) ;
m_ThdArray[i].nRLen = len ; //收取数据
m_ThdArray[i].strBuf.Empty() ; //保存收到数据
m_ThdArray[i].strSource = strSource ; //保存收到数据来源
m_ThdArray[i].pSock = pSock ;
m_ThdArray[i].nType = nType ;
if(m_CurrThreadCount <= 500 ) //并发线程不能超过500
{
//启动COMM的通讯处理线程
theApp.m_pComm->CreateCommThd((LPVOID)&(m_ThdArray[i])) ;
m_CurrThreadCount++ ;
m_ThdArray[i].unState = 1 ; //正在处理
}
return TRUE ;
}
}
return FALSE ;
}
//##ModelId=44B6F8830215
int CThdPool::AddCommItem(sCommEQ *pEQTemp, sCommPack InPack, CString strBuf, CString strSource)
{
//int i,iMax ;
sCommItem* pItem = new sCommItem ;
pItem->pEQ = pEQTemp ;
pItem->InPack = InPack ;
pItem->pSock = NULL ;
pItem->strBuf = strBuf ;
pItem->strSource = strSource ;
//通讯包处理
theApp.m_pComm->m_pProtocol->Transact( pItem ) ;
delete pItem ;
/*
iMax = m_ThdArray.GetSize() ;
for( i = 0 ; i < iMax ; i++ )
{
//-问题-存在清除处理线空间问题。
if( m_ThdArray[i].unState == 3 ) // 空闲线程
{
m_ThdArray[i].unID = i ;
m_ThdArray[i].unState = 0 ; // 0为未处理
m_ThdArray[i].pEQ = pEQTemp ; //设备指针
m_ThdArray[i].InPack = InPack ;
m_ThdArray[i].pSock = NULL ;
//复制通讯数据包
m_ThdArray[i].strBuf = strBuf ; //保存收到数据
m_ThdArray[i].strSource = strSource ; //保存收到数据来源
if(m_CurrThreadCount <= 500 ) //并发线程不能超过500
{
m_ThdArray[i].unState = 1 ; //正在处理
//pItem = &m_ThdArray[i] ;
//启动COMM的通讯线程
theApp.m_pComm->CreateCommThd((LPVOID)&(m_ThdArray[i])) ;
m_CurrThreadCount++ ;
}
return 0 ;
}
}
*/
return 0 ;
}
//##ModelId=44B6F8830204
int CThdPool::AddCommItem(CString strBuf, CString strSource,LPVOID pSock)
{
//int i,iMax ;
sCommItem* pItem = new sCommItem ;
pItem->pEQ = NULL ;
pItem->pSock = pSock ;
pItem->strBuf = strBuf ;
pItem->strSource = strSource ;
//通讯包处理
theApp.m_pComm->m_pProtocol->Transact( pItem ) ;
delete pItem ;
/*
iMax = m_ThdArray.GetSize() ;
for( i = 0 ; i < iMax ; i++ )
{
//-问题-存在清除处理线空间问题。
if( m_ThdArray[i].unState == 3 ) // 空闲线程
{
m_ThdArray[i].unID = i ;
m_ThdArray[i].pEQ = NULL ; //设备指针
m_ThdArray[i].strBuf = strBuf ; //保存收到数据
m_ThdArray[i].strSource = strSource ; //保存收到数据来源
m_ThdArray[i].pSock = pSock ;
m_ThdArray[i].unState = 0 ; // 0为未处理
if(m_CurrThreadCount <= 500 ) //并发线程不能超过500
{
m_ThdArray[i].unState = 1 ; //正在处理
//启动COMM的通讯线程
theApp.m_pComm->CreateCommThd((LPVOID)&(m_ThdArray[i])) ;
m_CurrThreadCount++ ;
}
return 0 ;
}
}
return -1 ;
*/
return 0 ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -