最近发现程序无法终止,查证发现问题出现在pthread_cancel上。加锁代码如下:
IAidTaskPtr CAidThreadPool::getTask() {
IAidTaskPtr pTask = NULL;
while (m_bRun)
{
pTask = NULL;
{
CAutoLock lock(m_BusinessLock);
if ( m_listTasks.empty() )
{
m_BusinessLock.wait();// pthread_cond_wait()实现
continue;
}
if (!m_listTasks.empty()) {
pTask = m_listTasks.front();
m_listTasks.pop_front();
break;
}
}
}
return pTask;
}
pthread_cancel 调用并不等待线程终止,它只提出请求。线程在取消请求(pthread_cancel)发出后会继续运行,直到到达某个取消点(Cancellation Point)。取消点是线程检查是否被取消并按照请求进行动作的一个位置。
pthread_cancel manual 说以下几个 POSIX 线程函数是取消点:
pthread_join(3)
pthread_cond_wait(3)
pthread_cond_timedwait(3)
pthread_testcancel(3)
sem_wait(3)
sigwait(3)
而当pthread_cancel时,程序正好处于pthread_cond_wait这个取消点上,但为什么还是发生了死锁呢?
在Linux的实现中在真正wait之前,有这样一段代码:
/* Before we block we enable cancellation. Therefore we have to install a cancellation handler. */
__pthread_cleanup_push (&buffer, __condvar_cleanup, &cbuffer);
而__condvar_cleanup,做了什么事情呢?
/* Get the mutex before returning unless asynchronous cancellation is in effect. */
__pthread_mutex_cond_lock (cbuffer->mutex);
}
加上了锁,而这段代码是在pthread_cancel发生时会执行到,这也就直接导致了死锁。
解决:不使用pthread_cancel终止线程
其他解决办法:
void cleanup(void *arg)
{
pthread_mutex_unlock(&mutex);
}
void* thread0(void* arg)
{
pthread_cleanup_push(cleanup, NULL); // thread cleanup handler
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond, &mutex);
pthread_mutex_unlock(&mutex);
pthread_cleanup_pop(0);
pthread_exit(NULL);
}
参考文献:
http://www.linuxalgorithm.com/1086654/