// ThreadManager.cpp // // Revision Log // // Date Who SAR Notes // ========== === ======= ===================================== // 2001-10-17 mph Revamp everything // 2000-12-21 mph Initial coding by Mark Henri of // MPH Software - markhenri@attbi.com // 2001-10-26 mph Change again; create global logic // // // #define IN_THREADMANAGER_CPP #include "ThreadManager.h" #include ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CThreadManager::CThreadManager() : active(0) { SetTypes(5); // for starters } CThreadManager::~CThreadManager() { //if (GetActiveThreads(-1) > 0) if (active) delete [] active; } void CThreadManager::SetTypes(int numberOfTypes) { { CLock lock(criticalSection); if (active) delete [] active; types = numberOfTypes; active = new int[types]; InitializeCounts(); } Changed(); } void CThreadManager::InitializeCounts() { // don't do this here CLock lock(criticalSection); for (int n=0; n < types; n++) active[n] = 0; } int CThreadManager::Increment(int threadType) { if (threadType < 0 || threadType >= types) threadType = 0; int value = 0; { CLock lock(criticalSection); value = ++(active[threadType]); } Changed(); return value; } int CThreadManager::Decrement(int threadType) { if (threadType < 0 || threadType >= types) threadType = 0; int value = 0; { CLock lock(criticalSection); value = --(active[threadType]); } Changed(); return value; } // a type of -1 means get totals for all types int CThreadManager::GetActiveThreads(int type) { CLock lock(criticalSection); if (type >= types) return -1; // error; out of range if (type >= 0 ) return active[type]; int total = 0; for (int n=0; n < types; n++) total += active[n]; return total; } bool CThreadManager::WaitForThreadsToTerminate(int SecondsMax) { DWORD milliSecondsWait = 500; DWORD milliSecondsMax = SecondsMax * 1000; DWORD milliSeconds = 0; while (GetActiveThreads() > 0) { Sleep(milliSecondsWait); milliSeconds += milliSecondsWait; if (milliSeconds > milliSecondsMax) return false; } return true; }