pthread_cancel引起的死锁

最近发现程序无法终止,查证发现问题出现在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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值