当前位置: 技术问答>linux和unix
如何不留后遗症地取消处于等待条件变量信号的线程?
来源: 互联网 发布时间:2015-12-24
本文导语: 如何取消处于等待条件变量信号的线程? -------------------------------------------------- 该线程ID为pid,其线程函数体如下: pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL); while(1) { pthread_mutex_lock(&mutex); pthread_cond_wait(&cond,...
如何取消处于等待条件变量信号的线程?
--------------------------------------------------
该线程ID为pid,其线程函数体如下:
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
while(1)
{
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond,&mutex);
//do some work
pthread_mutex_unlock(&mutex);
}
在网上看了一些资料,如设置取消点,pthread_cleanup_push,pthread_cleanup_pop,结果越看越糊涂,哪位高手帮忙将上面的线程函数体改一下,以便不留后遗症的响应pthread_cancel(pid)
谢谢了!
--------------------------------------------------
该线程ID为pid,其线程函数体如下:
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
while(1)
{
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond,&mutex);
//do some work
pthread_mutex_unlock(&mutex);
}
在网上看了一些资料,如设置取消点,pthread_cleanup_push,pthread_cleanup_pop,结果越看越糊涂,哪位高手帮忙将上面的线程函数体改一下,以便不留后遗症的响应pthread_cancel(pid)
谢谢了!
|
Here is how to lock a mutex mut in such a way that it will
be unlocked if the thread is canceled while mut is locked:
pthread_cleanup_push(pthread_mutex_unlock, (void *) &mut);
pthread_mutex_lock(&mut);
/* do some work */
pthread_mutex_unlock(&mut);
pthread_cleanup_pop(0);
Equivalently, the last two lines can be replaced by
pthread_cleanup_pop(1);
Notice that the code above is safe only in deferred can
cellation mode (see pthread_setcanceltype(3)). In asyn
chronous cancellation mode, a cancellation can occur
between pthread_cleanup_push and pthread_mutex_lock, or
between pthread_mutex_unlock and pthread_cleanup_pop,
resulting in both cases in the thread trying to unlock a
mutex not locked by the current thread. This is the main
reason why asynchronous cancellation is difficult to use.
be unlocked if the thread is canceled while mut is locked:
pthread_cleanup_push(pthread_mutex_unlock, (void *) &mut);
pthread_mutex_lock(&mut);
/* do some work */
pthread_mutex_unlock(&mut);
pthread_cleanup_pop(0);
Equivalently, the last two lines can be replaced by
pthread_cleanup_pop(1);
Notice that the code above is safe only in deferred can
cellation mode (see pthread_setcanceltype(3)). In asyn
chronous cancellation mode, a cancellation can occur
between pthread_cleanup_push and pthread_mutex_lock, or
between pthread_mutex_unlock and pthread_cleanup_pop,
resulting in both cases in the thread trying to unlock a
mutex not locked by the current thread. This is the main
reason why asynchronous cancellation is difficult to use.
|
So if the thread is waiting for the condition, it can not be cancelled. after the pthread_cond_wait() return with holding the lock, it will respond to the cancellation request.
Note - pthread_cond_wait() is a cancellation point. If a cancel is pending and
the calling thread has cancellation enabled, the thread terminates and begins
executing its cleanup handlers while continuing to hold the lock.
Your program is ok. At least in my opinion
Note - pthread_cond_wait() is a cancellation point. If a cancel is pending and
the calling thread has cancellation enabled, the thread terminates and begins
executing its cleanup handlers while continuing to hold the lock.
Your program is ok. At least in my opinion
|
封装个线程基类,析构时,关闭线程,就可以安全退出
可参考我的blog:http://blog.csdn.net/wqf363/
可参考我的blog:http://blog.csdn.net/wqf363/
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。