最近写了个线程池的程序,可是在最后线程函数一直无法退出 请各路大神帮忙解答一下。由于行数比较多就不全贴出来了。
线程池实现:
析构函数:
TthreadControl::~TthreadControl(){
while( numOfWaitJobs != 0 ){
printf("size: ", numOfWaitJobs);
//cout << "size:" << numOfWaitJobs << endl;
sleep(10);
}
printf("m_ShutDown is falst\n");
pthread_mutex_lock(&m_Mutex_lock);
m_ShutDown = true;
pthread_mutex_unlock(&m_Mutex_lock);
printf("m_ShutDown is true\n");
printf("before broadcast\n");
//cout << "before broadcast" << endl;
pthread_cond_broadcast( &m_cond ); //tell all thread the pool will be closed.
printf("after broadcast\n");
for( int index = 0; index < qThreadID.size(); index++ ){
printf("before join\n");
pthread_join( *qThreadID[index], NULL );
printf("after join\n");
}
printf("before free lock and con\n");
pthread_mutex_destroy( &(m_Mutex_lock) );
pthread_cond_destroy( &(m_cond) );
printf("after free lock and con\n");
}
线程函数:
void * TthreadControl::Controler( void * arg ){
TthreadControl * thread = ( TthreadControl * )arg;
pthread_t tid = pthread_self();
while( true ){
printf("thread id:%u. in controler before lock\n", tid);
pthread_mutex_lock( &(thread->m_Mutex_lock) ); //
while ( thread->numOfWaitJobs == 0 && thread->m_ShutDown == false ) {
printf("thread id:%u. in controler before wait\n", tid);
pthread_cond_wait( &(thread->m_cond), &(thread->m_Mutex_lock) );
printf("thread id:%u. in controler after wait\n", tid);
}
if ( thread->m_ShutDown ) {
printf("thread id:%u. in controler before unlock and will exit\n", tid);
pthread_mutex_unlock( &(thread->m_Mutex_lock) ); //?~W??~H?记?~O解?~T~A?~@~B
printf("thread id:%u. in controler after unlock and will exit\n", tid);
//return NULL;
pthread_exit( NULL );
}
assert( thread->numOfWaitJobs != 0 );
thread->numOfWaitJobs--;
threadWorker * worker = thread->qWaitjobs.at( 0 ); //?~O~V?~G?第?~@个?~E~C?| ?~@~B
thread->qWaitjobs.erase( thread->qWaitjobs.begin() );
printf("thread id:%u. in controler before unlock and will process worker\n", tid);
pthread_mutex_unlock( &(thread->m_Mutex_lock) ); //解?~T~A
printf("thread id:%u. in controler after unlock and will process worker\n", tid);
worker->m_Function( worker->m_Arg );
printf("thread id:%u. in controler after processed worker\n", tid);
}
}
线程函数调用的任务函数:
void LExpandSeed( TSeed seed, map * qExists, double fractionOfASingleSeed, long nTReads,vector * qSeed, vector * faFile, bool dir ){//if true, extend towards left; else towards right.
char qBase[] = {'A','T','G','C'};
string rawSeq = seed.GetSeq();
vector< long > * qSupSeqOfRawSeed = seed.GetSupSeq();
pthread_t pid = pthread_self();
bool ifExtend = false;
for( int index=0; index string newSeq;
if ( dir ) {
newSeq = qBase[index]+rawSeq;
} else {
newSeq = rawSeq + qBase[index];
}
//lock
pthread_mutex_lock( &mutexOfqExist );
printf("thread:%u locked qexists\n", pid);
if( qExists->find(newSeq) != qExists->end() ){
ifExtend = true; // the seed has been signed.
//unlock;
printf("thread:%u will unlocked qexists\n", pid);
pthread_mutex_unlock( &mutexOfqExist );
printf("thread:%u unlocked qexists\n", pid);
continue;
}
//create a son seed.
TSeed sonSeed( newSeq.c_str() );
qExists->insert( pair(newSeq,1) );
//unlock
printf("thread:%u will unlocked qexists\n", pid);
pthread_mutex_unlock( &mutexOfqExist );
printf("thread:%u unlocked qexists\n", pid);
for( int i=0; isize(); i++ ){
if ( faFile->at((*qSupSeqOfRawSeed)[i]).find(newSeq) != string::npos ) {
sonSeed.AddSupSeq( qSupSeqOfRawSeed->at(i) );
}
}
if ( (float)(sonSeed.GetSupNum())/nTReads > fractionOfASingleSeed ) {
LExpandSeed( sonSeed, qExists, fractionOfASingleSeed, nTReads, qSeed, faFile,dir );
ifExtend = true;
}
}
if ( !ifExtend ) {
printf("thread:%u will locked qseed\n", pid);
pthread_mutex_lock( &mutexOfRSeed );
printf("thread:%u locked qseed\n", pid);
qSeed->push_back( seed ); // qseed 是一个vector<string>类型的向量
printf("thread:%u will unlocked qseed\n", pid);
pthread_mutex_unlock( &mutexOfRSeed );
printf("thread:%u unlocked qseed\n", pid);
}
}
现在的具体情况是:每次调用调用析构的时候, 程序都会停在pthread_exit(NULL);这个地方 无法退出, 请教各路大神这个有可能是神马情况,怎么破??
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。