线程池管理

本文介绍了一个线程池管理系统的设计与实现,包括线程管理和任务调度等核心功能。系统支持线程的启动、终止及任务的添加与执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 申明
class CManageThread  
{
public:
CManageThread();
virtual ~CManageThread();
private:
HANDLE m_HThread;//线程句柄
HANDLE m_HEvent;//结束空闲线程通知
IThreadExcute* m_te;
CSemaphore *m_section;
public:
    bool ExcuteThread();
//-----------------------------------
bool PostExitThread();
bool TerminateExitThread();
//-----------------------------------
HANDLE GetThreadEvent();//获取通知句柄
HANDLE GetThreadHandle();//获取线程句柄
bool GetThreadIsLeisure();
    void SetThreadExcute(IThreadExcute *pte);
void SetThreadHandle(HANDLE handle);
//---------------------------------------------
//锁定当前线程
bool SetLockThread(int tm=0);
};

class CTemporarilyThread  
{
public:
CTemporarilyThread();
virtual ~CTemporarilyThread();
private:
    HANDLE m_HThread;
    IThreadExcute* m_te;
public:
bool TerminateExitThread();
bool ExcuteThread();
HANDLE GetThreadHandle();//获取线程句柄
    void SetThreadExcute(IThreadExcute *pte);
void SetThreadHandle(HANDLE handle);
};

class CThreadPoolManage  
{
private:
CThreadPoolManage();
virtual ~CThreadPoolManage();
private:
    static CThreadPoolManage* m_ChreadPoolManage;
static CArray<CTemporarilyThread*,CTemporarilyThread*> m_ATemporarily;
static CArray<CManageThread*,CManageThread*> m_AManageThread;
    static CSemaphore m_sem;
    static CCriticalSection section;
public:
    static CThreadPoolManage* GetThreadPoolManage();
static void CloseThreadPoolManage();
private:
//装载临时任务
static DWORD WINAPI ExcuteMission(LPVOID n);
    //装载空闲任务
static DWORD WINAPI ExcuteLeisureMission(LPVOID n);
private:
  
public:
    bool AddMission(IThreadExcute* pte);//添加任务
bool SatartTemporarilyThread(int count);//启动一到多个空闲线程
int CloseTemporarilyThread(int count);//关闭一到多个空闲线程
bool CloseTemporarilyThreadAll();//关闭所有空闲线程
int GetThreadPoolCount();//获取当前远行的线程数量    
};

class IThreadExcute  
{
public:
IThreadExcute();
virtual ~IThreadExcute();
public:
    virtual void ExcuteThread();
};

// 实现
CManageThread::CManageThread()
{
    m_HThread = NULL;
m_te = NULL;
m_HEvent = NULL;
m_section = new CSemaphore(1,1);
m_section->Unlock();
}
CManageThread::~CManageThread()
{
   delete m_section;
   CloseHandle(m_HEvent);
}
bool CManageThread::ExcuteThread()
{
   try
   {
      while(1)
   {
   if(WaitForSingleObject(m_HEvent,0)!=WAIT_TIMEOUT)
   {
   ResetEvent(m_HEvent);
            break;
   }
   try
   {
      if(m_te!=NULL)
         m_te->ExcuteThread();
   m_section->Unlock();
   }
         catch(...)
   {
      m_te = NULL;
   m_section->Unlock();
   }
   m_te = NULL;
   }    
   }
   catch(...)
   {
  ResetEvent(m_HEvent);
  m_section->Unlock();
  m_te = NULL;
     return false;
   }
   m_section->Unlock();
   m_te = NULL;
   return true;
}
bool CManageThread::PostExitThread()
{
try
{
  if(m_HEvent!=NULL)
     SetEvent(m_HEvent);
}
catch(...)
{
  return false;
}
return true;
}
bool CManageThread::TerminateExitThread()
{
   try  
   {
    if(m_HThread!=NULL)
     TerminateThread(m_HThread,0);
    m_section->Unlock();
   }
   catch(...)
   {
       m_section->Unlock();
    return false;
   }
   return true;
}
HANDLE CManageThread::GetThreadEvent()
{
return m_HEvent;
}
void CManageThread::SetThreadExcute(IThreadExcute *pte)
{
    m_te = pte;
}
HANDLE CManageThread::GetThreadHandle()//获取线程句柄
{
return m_HThread;
}
bool CManageThread::GetThreadIsLeisure()
{
return (m_te==NULL)?true:false;
}
void CManageThread::SetThreadHandle(HANDLE handle)
{
    m_HThread = handle;
CString EventStr;
EventStr.Format("%d",m_HThread);
m_HEvent = CreateEvent(NULL,true,false,EventStr);
ResetEvent(m_HEvent);
}
//锁定当前线程
bool CManageThread::SetLockThread(int tm)
{
return (m_section->Lock(tm)==0)?false:true;
}

CTemporarilyThread::CTemporarilyThread()
{
    m_HThread = NULL;
m_te = NULL;
}
CTemporarilyThread::~CTemporarilyThread()
{
}
bool CTemporarilyThread::TerminateExitThread()
{
   try
   {
      if(m_HThread!=NULL)
    TerminateThread(m_HThread,0);
   }
   catch(...)
   {
    return false;
   }
   return true;
}
bool CTemporarilyThread::ExcuteThread()
{
    try
{
  if(m_te!=NULL)
   m_te->ExcuteThread();
}
catch(...)
{
  return false;
}
return true;
}
HANDLE CTemporarilyThread::GetThreadHandle()//获取线程句柄
{
   return m_HThread;
}
void CTemporarilyThread::SetThreadExcute(IThreadExcute *pte)
{
   m_te = pte;
}
void CTemporarilyThread::SetThreadHandle(HANDLE handle)
{
m_HThread = handle;
}

CThreadPoolManage::CThreadPoolManage()
{
}  
CThreadPoolManage::~CThreadPoolManage()
{
}
CThreadPoolManage* CThreadPoolManage::m_ChreadPoolManage = NULL;
CArray<CManageThread*,CManageThread*> CThreadPoolManage::m_AManageThread;
CArray<CTemporarilyThread*,CTemporarilyThread*> CThreadPoolManage::m_ATemporarily;
//初始化信号量
CSemaphore CThreadPoolManage::m_sem(MAX_THREAD,MAX_THREAD);
CCriticalSection CThreadPoolManage::section;
CThreadPoolManage* CThreadPoolManage::GetThreadPoolManage()
{
   if(m_ChreadPoolManage==NULL)
   {
    m_ChreadPoolManage = new CThreadPoolManage();
    m_AManageThread.RemoveAll();
       m_ATemporarily.RemoveAll();
   }
   return m_ChreadPoolManage;
}
void CThreadPoolManage::CloseThreadPoolManage()
{
   delete m_ChreadPoolManage;
   m_ChreadPoolManage = NULL;
}
DWORD WINAPI CThreadPoolManage::ExcuteMission(LPVOID n)
{
CTemporarilyThread* pte = (CTemporarilyThread*)n;
CTemporarilyThread* ptef = NULL;
    int i = 0;
if(pte!=NULL)
{
  try
  {
         pte->ExcuteThread();
  }
  catch(...)
  {
  }
}
    if(pte!=NULL)
{
  try
  {
   while(section.Lock(10000)==0);
   for(i=0;i<m_ATemporarily.GetSize();i++)
   {
                ptef = m_ATemporarily.GetAt(i);
    if(pte->GetThreadHandle()==ptef->GetThreadHandle())
    {
                    m_ATemporarily.RemoveAt(i);
     delete ptef;
     break;
    }
   }
   section.Unlock();
  }
  catch(...)
  {
            section.Unlock();
  }
}
    m_sem.Unlock();
    pte = NULL;
ptef = NULL;
return 0;
}
//装载空闲任务
DWORD WINAPI CThreadPoolManage::ExcuteLeisureMission(LPVOID n)
{
   CRegKeyIni ini;
   int i = 0;
   CManageThread* pmt = NULL;
   CManageThread* pManageThread = (CManageThread*)n;
   if(pManageThread!=NULL)
   {
    try
    {
    pManageThread->ExcuteThread();
    }
    catch(...)
    {
    }
   }
//----------删除除在队列中的记录---------------------
   if(pManageThread != NULL)
   {
    while(section.Lock(10000)==0);
CString str2,str1;
CTime tm = CTime::GetCurrentTime();
str2.Format("%d:%d:%d",tm.GetHour(),tm.GetMinute(),tm.GetSecond());
str1.Format("%d===hhhhhhhhhhh",pManageThread->GetThreadEvent());
ini.WriteIniStringAndCurrentDir(str1,str2,"123456","wangwei.ini");
    try
    {
     for(i=0;i<m_AManageThread.GetSize();i++)
     {
      pmt = m_AManageThread.GetAt(i);
      if(pmt!=NULL)      
        if(pmt->GetThreadHandle()==pManageThread->GetThreadHandle())
     {
                    m_AManageThread.RemoveAt(i);
     CString str,str1;
     CTime tm = CTime::GetCurrentTime();
     str1.Format("%d:%d:%d",tm.GetHour(),tm.GetMinute(),tm.GetSecond());
     str.Format("%d",pmt->GetThreadEvent());
     ini.WriteIniStringAndCurrentDir(str,str1,"ww","tt.ini");
                    delete pmt;    
     break;
     }      
     }
           section.Unlock();
    }
    catch(...)
    {
           section.Unlock();
    }
   }
   m_sem.Unlock();
   pmt = NULL;
   pManageThread = NULL;
//---------------------------------------------------
   return 0;
}
bool CThreadPoolManage::AddMission(IThreadExcute* pte)
{
CTemporarilyThread* ptt = NULL;
CManageThread* pmt = NULL;
    int i = 0;
DWORD id = 0;
HANDLE handle = NULL;
if(m_sem.Lock()==0)//任务已满
  return false;
try
{
//-----------查询是否存在空闲线程------------------
  if(section.Lock(10000)!=0)
  {
   for(i=0;i<m_AManageThread.GetSize();i++)
   {
               pmt = (CManageThread*)m_AManageThread.GetAt(i);            
      if(pmt->SetLockThread())
      {
                  pmt->SetThreadExcute(pte);
      break;
      }
   }
   if(i==m_AManageThread.GetSize())//不存在已经启动空闲线程
   {
                ptt = new CTemporarilyThread();
    handle = CreateThread(NULL,NULL,ExcuteMission,(LPVOID)ptt,CREATE_SUSPENDED,&id);
    ptt->SetThreadHandle(handle);
    ptt->SetThreadExcute(pte);
       m_ATemporarily.Add(ptt);
                ResumeThread(handle);
   }
   section.Unlock();
  }
  else return false;
//-------------------------------------------------
}
catch(...)
{
  section.Unlock();
  return false;
}
return true;
}
bool CThreadPoolManage::SatartTemporarilyThread(int count)//启动一到多个空闲线程
{
   int i = 0;
   CManageThread* pmt = NULL;
   HANDLE handle = NULL;
   DWORD id = 0;
   if(count>(MAX_LEISURE-m_AManageThread.GetSize()))
    return false;
   for(i=0;i<count;i++)
   {
      pmt = new CManageThread();
      handle = CreateThread(NULL,NULL,ExcuteLeisureMission,(LPVOID)pmt,CREATE_SUSPENDED,&id);
   pmt->SetThreadHandle(handle);
   m_AManageThread.Add(pmt);
   ResumeThread(handle);  
   }
   return true;
}
//关闭一到多个空闲线程,返回实际关闭的空闲线程数量
int CThreadPoolManage::CloseTemporarilyThread(int count)
{
   int i = 0;
   int js = 0;
   CManageThread* pmt = NULL;
   if(count > m_AManageThread.GetSize())
    return -1;
   if(section.Lock(10000)!=0)
   {
    try
    {
     for(i=(count-1);i>=0;i--)
     {
     pmt = m_AManageThread.GetAt(i);
     if(pmt->SetLockThread())
     {
     if(pmt->PostExitThread())
     {
      if(!pmt->SetLockThread(1000))
      {
       pmt->TerminateExitThread();
       m_AManageThread.RemoveAt(i);
       delete pmt;
      }
     }
     js++;
     }
     }
          section.Unlock();
    }
    catch(...)
    {
          section.Unlock();
    }
   }
   return js;
}
bool CThreadPoolManage::CloseTemporarilyThreadAll()//关闭所有线程池线程
{
   CRegKeyIni ini;
   CTemporarilyThread* ptt = NULL;
   CManageThread* pmt = NULL;
   int i = 0;
   int count = 0;
   int js = 0;
   while(section.Lock(10000)==0);
CString str2;
CTime tm = CTime::GetCurrentTime();
str2.Format("%d:%d:%d",tm.GetHour(),tm.GetMinute(),tm.GetSecond());
ini.WriteIniStringAndCurrentDir("HHHHHHHHH",str2,"123456","wangwei.ini");
   try
   {
       count = m_AManageThread.GetSize();
    for(i=(count-1);i>=0;i--)
    {
           pmt = m_AManageThread.GetAt(i);
           if(pmt->PostExitThread())
     {
      if(!pmt->SetLockThread(1000))
      {
                   pmt->TerminateExitThread();
       m_AManageThread.RemoveAt(i);
     CString str,str1;
     CTime tm = CTime::GetCurrentTime();
     str1.Format("%d:%d:%d",tm.GetHour(),tm.GetMinute(),tm.GetSecond());
     str.Format("%d",pmt->GetThreadEvent());
     ini.WriteIniStringAndCurrentDir(str,str1,"123456","tt.ini");
       delete pmt;      
      }
      js++;
     }
    }
   CString str;
   str.Format("%d",js);
   ::AfxMessageBox(str);
       count = m_ATemporarily.GetSize();
    for(i=(count-1);i>=0;i--)
    {
           ptt = m_ATemporarily.GetAt(i);
     ptt->TerminateExitThread();
     m_ATemporarily.RemoveAt(i);
     delete ptt;
    }
    section.Unlock();
   }
   catch(...)
   {
    section.Unlock();
    return false;
   }
   return true;
}
int CThreadPoolManage::GetThreadPoolCount()//获取当前远行的线程数量
{
   return m_AManageThread.GetSize()+m_ATemporarily.GetSize();
}

注:CManageThread负责管理单个线程,CThreadPoolManage负责管理线程池,单例实现

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值