/* Description : Use this method to destory the thread pool. The destructor of this class will destory the pool anyway. Make sure you this method before calling a Create() when an existing pool is already existing. Returns : void *****************************************************************************/ void CThreadPool::Destroy() { // Set the state to 'destroying'. Pooled functions might be checking this // state to see if they need to stop. m_bPoolIsDestroying = true; // tell all threads to shutdown. if(!m_hNotifyShutdown) { return; } //ASSERT(NULL != m_hNotifyShutdown); SetEvent(m_hNotifyShutdown); //Sleep(1000); // give the threads a chance to complete if(GetWorkingThreadCount() > 0) { // There are still threads in processing mode.. // lets give the thread one last chance to finish based on configured setting Sleep(m_nWaitForThreadsToDieMS); } ThreadMap::iterator iter; _ThreadData ThreadData; // walk through the events and threads and close them all for(iter = m_threads.begin(); iter != m_threads.end(); iter++) { ThreadData = (*iter).second; CloseHandle(ThreadData.WaitHandle); CloseHandle(ThreadData.hThread); } // close the shutdown event CloseHandle(m_hNotifyShutdown); m_hNotifyShutdown = NULL; // free any memory not released FunctionList::iterator funcIter; for(funcIter = m_functionList.begin(); funcIter != m_functionList.end(); funcIter++) { if(funcIter->pData != NULL) { delete funcIter->pData; funcIter->pData = NULL; } } // empty all collections m_functionList.clear(); m_threads.clear(); }
/* Description : Use this method to destory the thread pool. The destructor of this class will destory the pool anyway. Make sure you this method before calling a Create() when an existing pool is already existing. Returns : void *****************************************************************************/ void CThreadPool::Destroy() { if(m_state == Destroying || m_state == Destroyed) return; // Set the state to 'destroying'. Pooled functions might be checking this // state to see if they need to stop. m_state = Destroying; // tell all threads to shutdown. SetEvent(m_hNotifyShutdown); Sleep(2000); // give the threads a chance to complete if(GetWorkingThreadCount() > 0) { // There are still threads in processing mode.. // lets give the thread one last chance to finish based on configured setting Sleep(m_nWaitForThreadsToDieMS); } ThreadMap::iterator iter; _ThreadData ThreadData; // walk through the events and threads and close them all for(iter = m_threads.begin(); iter != m_threads.end(); iter++) { CloseHandle(iter->second.WaitHandle); CloseHandle(iter->second.hThread); } // close the shutdown event CloseHandle(m_hNotifyShutdown); m_hNotifyShutdown = NULL; ReleaseMemory(); // free any remaining UserPoolData objects InterlockedDecrement(&CThreadPool::m_lInstance); m_state = Destroyed; }