开发者社区> 问答> 正文

关于线程无法退出的问题。

最近写了个线程池的程序,可是在最后线程函数一直无法退出 请各路大神帮忙解答一下。由于行数比较多就不全贴出来了。
线程池实现:
析构函数:

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);这个地方 无法退出, 请教各路大神这个有可能是神马情况,怎么破??

展开
收起
a123456678 2016-03-04 13:25:35 2343 0
1 条回答
写回答
取消 提交回答
  • 使用pthread_Wait ,等待线程结束.....即可

    2019-07-17 18:52:08
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
多IO线程优化版 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载