⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit_threads.cpp

📁 一个完成端口的框架程序
💻 CPP
字号:
//---------------------------------------------------------------------------


#pragma hdrstop

#include "Unit_Threads.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)
/*For working Threads*/
//添加工作线程
//Count:预添加的线程数量,默认值为1(见头文件)
bool __fastcall TtcpIOCP::AddThread(const UINT Count)
{
   UINT i=Count;
   while(i>0)
   {
	  if(!NewThread())
	  return false;
	  i--;
   }
   return true;
}

//Create a new working thread
//调用_beginthreadex创建新线程
//并把线程句柄(该句柄对线程完全的控制权)添加到工作线程列表当中
//同时把线程的ID号也列入工作线程的ID列表当中
//由于特别是线程内部不方便使用权OpenThread打开当前线程的句柄
//所以要对线程进行操作,可以通过GetCurrentThreadID()
//得到自己的线程ID,并查询相应的对该线程有完全控制权的句柄进行操作
bool __fastcall TtcpIOCP::NewThread(void)
{
   unsigned ThreadID;
   HANDLE pThread=(HANDLE)_beginthreadex(NULL,
                                         0,
                                         WorkerThread,
                                         this,
                                         CREATE_SUSPENDED,
                                         &ThreadID);
   if(!pThread)//创建结程成功则返回结程的句柄,否则返回NULL
	  return false;

   //进入工作线程列表操作临界区,以准备对结程列表进行成员增入
   EnterCriticalSection(&thread_CriticalSection);
   //使用try... __finally...配对以达使得LeaveCriticalSection能保证运行
   /************************************************************/
   /*  但是须注意的是EnterCriticalSection并不能保证一定会成功  */
   /*  并且该调用是地返回值的,所以使用时还是会存在相关风险性  */
   /************************************************************/
   try
   {
	  //线程句柄添加到列表末尾
	  FThreadList.insert(FThreadList.end(),pThread);
	  //线程ID添加到列表末尾
	  FThreadIDList.insert(FThreadIDList.end(),ThreadID);
   }
   __finally
   {
	  LeaveCriticalSection(&thread_CriticalSection);
   }
   ResumeThread(pThread);
   return true;
}
//cleanup all the working threads
void __fastcall TtcpIOCP::CleanThreadList(void)
{
   if(FThreadList.empty())
	  return;
   EnterCriticalSection(&thread_CriticalSection);
   try
   {
      UINT InternalCount=FThreadList.size();
      for(UINT i=0;i<InternalCount;i++)
      {
         PostQueuedCompletionStatus(FHandle,0,0,NULL);
      }
      WaitForMultipleObjects(InternalCount,&FThreadList[0],true,INFINITE);
      for_each(FThreadList.begin(),FThreadList.end(),CloseHandle);
      FThreadList.clear();
      FThreadIDList.clear();
   }
   __finally
   {
      LeaveCriticalSection(&thread_CriticalSection);
   }
}
/*For working Threads*/
//set a user expect number of working thread
//it must less then or equal to FThreadMax
bool __fastcall TtcpIOCP::SetExpectThread(const UINT Count)
{
   UINT InternalCount=Count;
   if(InternalCount<1)
      return false;
   if(InternalCount>MAX_THREAD_NUMBER)
      InternalCount=MAX_THREAD_NUMBER;
   FExpectThreadCount=InternalCount;
   if(ThreadCount==0&&TuningListenState==LS_WORK)
      AddThread(1);
   return true;
}
void __fastcall TtcpIOCP::DumpThread(const DWORD ThreadID,
                                     const bool NotInCriticalSection)
{
   HANDLE ThreadHandle;
   if(true==NotInCriticalSection)
   EnterCriticalSection(&thread_CriticalSection);
   try
   {
          VTCThreadID::iterator pThreadID;
          pThreadID=find(FThreadIDList.begin(),
                         FThreadIDList.end(),
                         ThreadID);
          if(pThreadID==FThreadIDList.end())
             return;
          ThreadHandle=*(FThreadList.begin()+(pThreadID-FThreadIDList.begin()));
          FThreadList.erase(FThreadList.begin()+(pThreadID-FThreadIDList.begin()));
          FThreadIDList.erase(pThreadID);
   }
   __finally
   {
       LeaveCriticalSection(&thread_CriticalSection);
   }
   CloseHandle(ThreadHandle);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -