当前位置:我的异常网» VC/MFC » 完成端口+线程池解决方案
完成端口+线程池解决方案(2)
www.myexceptions.net 网友分享于:2015-08-26 浏览:302次
}
if (i == 9)
{
::TerminateThread(m_hManagerThread, 0);
}
else
{
::Sleep(1000);
}
}
// close the CompletionPort
::CloseHandle(m_hManagerIO);
m_hManagerIO = NULL;
// end the thread in the pool
for (int i = 0; i < m_wMaxThreadNum; i++)
{
if (m_pThreadInfo[i].dwThreadID == 0)
{
continue;
}
m_pThreadInfo[i].bIsQuit = true;
for (int j = 0; j < 10; j++)
{
::GetExitCodeThread(m_pThreadInfo[i].hThreadHandle, &dwRes);
if (dwRes == CThreadPool::WORKERPROC_RETURN_VALUE)
{
break;
}
if (j == 9)
{
::TerminateThread(m_pThreadInfo[i].hThreadHandle, 0);
}
else
{
::Sleep(500);
}
}
}
::CloseHandle(m_hWorkerIO);
m_hWorkerIO = NULL;
if (m_pThreadInfo)
{
delete [] m_pThreadInfo;
m_pThreadInfo = NULL;
}
unsigned long pN1;
unsigned long pN2;
OVERLAPPED*pOverLapped;
while (::GetQueuedCompletionStatus(m_hWorkerIO, &pN1, &pN2, &pOverLapped, 0)) //m_hManagerIO
{
CWork* pWork = reinterpret_cast (pN1);
CWorkDesc* pWorkDesc = reinterpret_cast (pN2);
delete pWorkDesc;
}
::LeaveCriticalSection(&m_csLock);
}
unsigned __stdcall CThreadPool::ManagerProc(void* pThread)
{
unsigned long pN1;
unsigned long pN2;
OVERLAPPED* pOverLapped;
CThreadPool* pServer = reinterpret_cast (pThread);
while (!pServer-> m_bQuitManager)
{
if (::GetQueuedCompletionStatus(
pServer-> m_hManagerIO,
&pN1,
&pN2,
&pOverLapped,
pServer-> m_dwMSeconds)
== TRUE)
{
if (pOverLapped == (OVERLAPPED*)0xFFFFFFFFF)
{
break;
}
}
else
{
EThreadStatus stat = pServer-> GetWorkThreadStatus();
if (stat == CThreadPool::BUSY)
{
puts( "Add thread ");
pServer-> AddThread();
}
else if (stat == CThreadPool::IDLE)
{
puts( "Del thread ");
pServer-> DelThread();
}
else
{}
}
}
_endthreadex(CThreadPool::MANAGERPROC_RETURN_VALUE);
return CThreadPool::MANAGERPROC_RETURN_VALUE;
}
unsigned __stdcall CThreadPool::WorkerProc(void* pThread)
{
unsigned long pN1;
unsigned long pN2;
OVERLAPPED* pOverLapped;
CThreadPool* pServer = reinterpret_cast (pThread);
DWORD threadID = ::GetCurrentThreadId();
int nSeq = pServer-> GetThreadbyID(threadID);
if (nSeq < 0)
文章评论