I'm writing a thread pool class containing the following:
Code:
class ThreadPoolWorker {
friend class ThreadPool ;
public:
virtual int initialize() = 0 ;
virtual int finalize() = 0 ;
private:
pthread_t tid_;
pthread_attr_t attr_;
} ;
class ThreadPoolJob {
public:
virtual int run( ThreadPoolWorker* wrk) = 0 ; // pure virtual function
};
class ThreadPool {
public:
int start( ThreadPoolWorker *tpw, int threads) ; // Creates threads
bool add_work( ThreadPoolJob* job) ; // Put work onto the Q
private:
static int thread(void*) ; // thread
ThreadPoolWorker* twork; // array of workers
};
And the main code from the user perspective
Code:
// user classes
class UserThreadWorker : ThreadPoolWorker {
public:
int initialize ( ) {
data = 5; ;
private:
int data;
};
int
main() {
UserThreadWorker utw[5];
ThreadPoolWorker* tpw = utw;
ThreadPool tp;
tp.start(tpw, 5) ;
}
Now here is where I've traced the problem too.
&tpw[1] does not equal &utw[1].
Internally the start code uses loops through the tpw parameters accessing
and accesses the tpw[i] variable. But the second element address is off in the array
causing a segmentation violation when calling tpw[i]->initialize() ;
Are there any simple ways to correct this?
My goal is that the workers will have private data that will be utilized in the threads,
Threads will remain active, but will not do anything until work is posted into the ThreadPool.
Thanks for any suggestions.
Ken