Hi,
I was working on thread pool, which I used std::vector to implement the pool.
private:
std::vector <handlerthread*> _pool;
handlerthread* temp;
void threadpool::addWorker() {
handlerthread *temp;
temp = new handlerthread(this);
int x=_pool.size();
temp->start(&x);
_pool.push_back(temp);
}
handlerthread* threadpool::getIdleWorker(){
int idx=-1;
for (int i=0;i<_pool.size();i++){
temp=(handlerthread*)_pool[i]; //SIGSEGV HERE
if(temp->isIdle()){
idx=i;
break;
}
}
if (idx < 0) {
printf("need more worker!");
addWorker();
idx = _pool.size() - 1;
}
return _pool[idx];
}
Segmentation fault (SIGSEGV) is always received when I take back the thread from the vector.
what I want is to choose and use previously created thread, not creating a new one by copying the thread previously created. So I thought I should use
vector <handlerthread*> _pool
rather than
vector <handlerthread> _pool
.
Please give me some light.
Thanks a bunch!