pthread_cancel引起的死锁
2014-09-11 15:30
351 查看
最近发现程序无法终止,查证发现问题出现在pthread_cancel上。加锁代码如下:
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之前,有这样一段代码:
而__condvar_cleanup,做了什么事情呢?
解决:不使用pthread_cancel终止线程
其他解决办法:
参考文献:
http://www.linuxalgorithm.com/1086654/
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/
相关文章推荐
- pthread_cancel引起的死锁
- pthread_cancel引起线程死锁-linux
- 一个 pthread_cancel 引起的线程死锁【整理转载】
- 一个 pthread_cancel 引起的线程死锁
- 6、一个 pthread_cancel 引起的线程死锁(转)
- 6、一个 pthread_cancel 引起的线程死锁【整理转载】
- 一个 pthread_cancel 引起的线程死锁【整理转载】
- 6、一个 pthread_cancel 引起的线程死锁【整理转载】
- 防止死锁:采用linux多线程的pthread_cancel终止线程
- 用gdb脚本解决死锁的调试方法(由pthread_mutex_lock引起的死锁)
- linux下记一次使用gdb对死锁问题的定位以及pthread_cancel使用的建议
- pthread_cancel的带来的一个死锁问题(验证)
- 一个 pthread_cancel 引起的线程死锁
- pthread_kill和pthread_cancel
- pthread_cancel
- err=pthread_cancel(tid1);
- Release版本下pthread_mutex_t死锁分析
- 线程取消(pthread_cancel)
- HeapAlloc引起的死锁
- 工作线程操作主界面控件引起死锁及解决