zh_peng100 2013-06-27 09:10 采纳率: 0%
浏览 2443

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

最近写了个线程池的程序,可是在最后线程函数一直无法退出 请各路大神帮忙解答一下。由于行数比较多就不全贴出来了。
线程池实现:
析构函数:
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);这个地方 无法退出, 请教各路大神这个有可能是神马情况,怎么破??

  • 写回答

2条回答 默认 最新

  • oyljerry 2014-12-28 08:03
    关注

    线程函数中是否返回。然后线程才能结束
    可以用wait

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)