/* * cprDestroyThread * * Destroys the thread passed in. * * Parameters: thread - thread to destroy. * * Return Value: Success or failure indication. * In CNU there will never be a success * indication as the calling thread will * have been terminated. */ cprRC_t cprDestroyThread(cprThread_t thread) { cprRC_t retCode = CPR_FAILURE; static const char fname[] = "cprDestroyThread"; cpr_thread_t *cprThreadPtr; cprThreadPtr = (cpr_thread_t*)thread; if (cprThreadPtr != NULL) { CWinThread * pCWinThread; uint32_t result = 0; uint32_t waitrc = WAIT_FAILED; pCWinThread = (CWinThread *)((cpr_thread_t *)thread)->u.handlePtr; if (pCWinThread !=NULL) { result = pCWinThread->PostThreadMessage(WM_CLOSE, 0, 0); if(result) { waitrc = WaitForSingleObject(pCWinThread->m_hThread, 60000); } } if (result == 0) { CPR_ERROR("%s - Thread exit failure %d\n", fname, GetLastError()); retCode = CPR_FAILURE; } retCode = CPR_SUCCESS; /* Bad application! */ } else { CPR_ERROR("%s - NULL pointer passed in.\n", fname); retCode = CPR_FAILURE; } cpr_free(cprThreadPtr); return (retCode); };
//-------------------------------------------------------------------------------- int CThreadPool::ExitInstance() { MSG msg; while(PeekMessage(&msg, (HWND) -1, TPMSG_MESSAGE, TPMSG_MESSAGE, PM_REMOVE)) delete (CThreadPoolMsg*) msg.wParam; CSingleLock lock(&m_waiting.m_mutex, false); if(! lock.Lock(INFINITE)) return -1; CSingleLock lock2(&m_active.m_mutex, false); if(! lock2.Lock(INFINITE)) return -1; // stop all threads // wait for them to exit int nSizeA = m_active.GetCount(); int nSizeW = m_waiting.GetCount(); int nSize = nSizeA + nSizeW; if(nSize > 0) { POSITION pos = m_active.GetHeadPosition(); HANDLE* pHands = new HANDLE[nSize]; CWinThread* pThread = NULL; for(int i = 0; i < nSizeA; i++) { pThread = m_active.GetNext(pos); pHands[i] = pThread->m_hThread; pThread->PostThreadMessage(WM_QUIT, 0, 0); } pos = m_waiting.GetHeadPosition(); for(; i < nSize; i++) { pThread = m_waiting.GetNext(pos); pHands[i] = pThread->m_hThread; pThread->PostThreadMessage(WM_QUIT, 0, 0); } ::WaitForMultipleObjects(nSize, pHands, true, 60000); } return CWinThread::ExitInstance(); }
//-------------------------------------------------------------------------------- void CThreadPool::DoMsg(WPARAM wMsg, LPARAM) { // attempt to move a thread from waiting to active, then send it the msg // if there are no avail waiting threads, then keep sending ourselves the msg // until a thread becomes available CThreadPoolMsg* pMsg = (CThreadPoolMsg*) wMsg; CWinThread* pThread = GetNextWaiting(); if(pThread == NULL) { ::Sleep(20); PostThreadMessage(TPMSG_MESSAGE, (WPARAM) pMsg, 0); return; } SetThreadActive(pThread); pThread->PostThreadMessage(pMsg->m_nMsg, pMsg->m_wparam, pMsg->m_lparam); delete pMsg; }