问题描述
- 关于线程无法退出的问题。
- 最近写了个线程池的程序,可是在最后线程函数一直无法退出 请各路大神帮忙解答一下。由于行数比较多就不全贴出来了。
线程池实现:
析构函数:
TthreadControl::~TthreadControl(){while( numOfWaitJobs != 0 ){ printf(""size: "" numOfWaitJobs); //cout << ""size:"" << numOfWaitJobs << endl; sleep(10);}printf(""m_ShutDown is falstn"");pthread_mutex_lock(&m_Mutex_lock);m_ShutDown = true;pthread_mutex_unlock(&m_Mutex_lock);printf(""m_ShutDown is truen"");printf(""before broadcastn"");//cout << ""before broadcast"" << endl;pthread_cond_broadcast( &m_cond ); //tell all thread the pool will be closed.printf(""after broadcastn"");for( int index = 0; index < qThreadID.size(); index++ ){ printf(""before joinn""); pthread_join( *qThreadID[index] NULL ); printf(""after joinn"");}printf(""before free lock and conn"");pthread_mutex_destroy( &(m_Mutex_lock) );pthread_cond_destroy( &(m_cond) );printf(""after free lock and conn"");
}
线程函数:
void * TthreadControl::Controler( void * arg ){
TthreadControl * thread = ( TthreadControl * )arg;
pthread_t tid = pthread_self();
while( true ){
printf(""thread id:%u. in controler before lockn"" tid);
pthread_mutex_lock( &(thread->m_Mutex_lock) ); //
while ( thread->numOfWaitJobs == 0 && thread->m_ShutDown == false ) {
printf(""thread id:%u. in controler before waitn"" tid);
pthread_cond_wait( &(thread->m_cond) &(thread->m_Mutex_lock) );
printf(""thread id:%u. in controler after waitn"" tid);
}
if ( thread->m_ShutDown ) {
printf(""thread id:%u. in controler before unlock and will exitn"" tid);
pthread_mutex_unlock( &(thread->m_Mutex_lock) ); //?~W??~H?记?~O解?~T~A?~@~B
printf(""thread id:%u. in controler after unlock and will exitn"" 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 workern"" tid); pthread_mutex_unlock( &(thread->m_Mutex_lock) ); //解?~T~A printf(""thread id:%u. in controler after unlock and will process workern"" tid); worker->m_Function( worker->m_Arg ); printf(""thread id:%u. in controler after processed workern"" tid);}
}
线程函数调用的任务函数:
void LExpandSeed( TSeed seed map * qExists double fractionOfASingleSeed long nTReadsvector * 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 qexistsn"" pid);
if( qExists->find(newSeq) != qExists->end() ){
ifExtend = true; // the seed has been signed.
//unlock;
printf(""thread:%u will unlocked qexistsn"" pid);
pthread_mutex_unlock( &mutexOfqExist );
printf(""thread:%u unlocked qexistsn"" pid);
continue;
}
//create a son seed.
TSeed sonSeed( newSeq.c_str() );
qExists->insert( pair(newSeq1) );
//unlock
printf(""thread:%u will unlocked qexistsn"" pid);
pthread_mutex_unlock( &mutexOfqExist );
printf(""thread:%u unlocked qexistsn"" 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 faFiledir );
ifExtend = true;
}}if ( !ifExtend ) { printf(""thread:%u will locked qseedn"" pid); pthread_mutex_lock( &mutexOfRSeed ); printf(""thread:%u locked qseedn"" pid); qSeed->push_back( seed ); // qseed 是一个vector<string>类型的向量 printf(""thread:%u will unlocked qseedn"" pid); pthread_mutex_unlock( &mutexOfRSeed ); printf(""thread:%u unlocked qseedn"" pid);}
}
现在的具体情况是:每次调用调用析构的时候 程序都会停在pthread_exit(NULL);这个地方 无法退出, 请教各路大神这个有可能是神马情况,怎么破??
解决方案
线程函数中是否返回。然后线程才能结束
可以用wait
解决方案二:
可以用 pthread_Wait 等待线程结束.....