1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
/*
* Modification History
*
* 2002-March-12 Jason Rohrer
* Created.
*
* 2002-April-5 Jason Rohrer
* Changed to extend StopSignalThread.
*
* 2002-August-6 Jason Rohrer
* Changed member init order.
*
* 2003-September-5 Jason Rohrer
* Moved into minorGems.
*/
#include "ThreadHandlingThread.h"
ThreadHandlingThread::ThreadHandlingThread()
: mThreadVector( new SimpleVector<RequestHandlingThread*>() ),
mLock( new MutexLock() ) {
this->start();
}
ThreadHandlingThread::~ThreadHandlingThread() {
stop();
join();
mLock->lock();
int numThreads = mThreadVector->size();
// join each thread and delete it
for( int i=0; i<numThreads; i++ ) {
RequestHandlingThread *thread =
*( mThreadVector->getElement( i ) );
thread->join();
delete thread;
}
delete mThreadVector;
mLock->unlock();
delete mLock;
}
void ThreadHandlingThread::addThread(
RequestHandlingThread *inThread ) {
mLock->lock();
mThreadVector->push_back( inThread );
mLock->unlock();
}
void ThreadHandlingThread::run() {
while( !isStopped() ) {
// sleep for 5 seconds
sleep( 5000 );
// printf( "Thread handler looking for finished threads\n" );
// look for threads that need to be deleted
mLock->lock();
char threadFound = true;
// examine each thread
while( threadFound ) {
threadFound = false;
int numThreads = mThreadVector->size();
for( int i=0; i<numThreads; i++ ) {
RequestHandlingThread *thread =
*( mThreadVector->getElement( i ) );
if( thread->isDone() ) {
// join the thread before destroying it
// to prevent memory leaks
thread->join();
// remove the thread from the vector and delete it
// printf( "deleting a thread\n" );
mThreadVector->deleteElement( i );
delete thread;
threadFound = true;
// jump out of the for loop, since our
// vector size has changed
i = numThreads;
}
}
}
mLock->unlock();
}
}
|