java 线程池 完成端口,完成端口+线程池解决方案(2)

当前位置:我的异常网» 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)

文章评论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值