ThreadPool.cxx

Go to the documentation of this file.
00001 #include "ThreadPool.hxx"
00002 #include "Thread.hxx"
00003 #include "PooledThread.hxx"
00004 #include <algorithm>
00005 
00006 namespace CLAM
00007 {
00008 
00009 ThreadPool::ThreadPool(int argInitialNumberOfThreads, bool argIsRealtime)
00010         : mIsRealtime(argIsRealtime)
00011 {
00012         for (int counter = 0; counter < argInitialNumberOfThreads; counter++)
00013         {
00014                 idleThreads.push_back( new CLAM::PooledThread(this, mIsRealtime) );
00015         }
00016 }
00017 ThreadPool::~ThreadPool()
00018 {
00019         EmptyPool();
00020 }
00021 
00022 void ThreadPool::EmptyPool()
00023 {
00024         Mutex::ScopedLock lock( dequeMutex );
00025 
00026         // First we stop and delete all the busy threads
00027         for (int counter = 0; counter < busyThreads.size(); counter++)
00028         {
00029                 PooledThread* threadPtr = busyThreads.at( counter );
00030                 if (threadPtr != NULL)
00031                 {
00032                         if (threadPtr->IsRunning())
00033                                 threadPtr->Stop();
00034                         delete threadPtr;
00035                 }
00036         }
00037         busyThreads.clear();
00038 
00039         // Next we delete all the idle threads
00040         for (int counter = 0; counter < idleThreads.size(); counter++)
00041         {
00042 
00043                 PooledThread* threadPtr = idleThreads.at( counter );
00044                 if (threadPtr != NULL)
00045                 {
00046                         delete threadPtr;
00047                 }
00048         }
00049         idleThreads.clear();
00050 }
00051 
00052 PooledThread* ThreadPool::GetThreadFromPool()
00053 {
00054         Mutex::ScopedLock lock( dequeMutex );
00055 
00056         // are there any more idle threads?
00057         if (idleThreads.size() == 0)
00058         {
00059                 // check to see if any of the threads in the busy queue are actually idle
00060                 bool foundIdleThread = false;
00061                 std::deque<PooledThread*>::iterator iter;
00062                 for (iter = busyThreads.begin(); iter < busyThreads.end(); iter++)
00063                 {
00064                         PooledThread* threadPtr = *iter;
00065                         if ( !threadPtr->IsRunning() )
00066                         {
00067                                 busyThreads.erase(iter);
00068                                 idleThreads.push_back(threadPtr);
00069                                 foundIdleThread = true;
00070                         }
00071                 }
00072 
00073                 if ( !foundIdleThread )
00074                 {
00075                         idleThreads.push_back( new CLAM::PooledThread(this, mIsRealtime) );
00076                 }
00077         }
00078 
00079         PooledThread* threadPtr = idleThreads.at(0);
00080         idleThreads.pop_front();
00081         busyThreads.push_back(threadPtr);
00082 
00083         return threadPtr;
00084 }
00085 
00086 void ThreadPool::ReturnThreadToPool(PooledThread* argThreadPtr)
00087 {
00088         Mutex::ScopedLock lock( dequeMutex );
00089 
00090         std::deque<PooledThread*>::iterator iter = find ( busyThreads.begin(), busyThreads.end(), argThreadPtr );
00091         busyThreads.erase( iter);
00092 
00093         idleThreads.push_back( argThreadPtr );
00094 }
00095 
00096 } // end namespace CLAM
Generated by  doxygen 1.6.3