了pthread_exit()的信号处理程序 [英] pthread_exit() in signal handler

查看:275
本文介绍了了pthread_exit()的信号处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(这个问题可能有所涉及<一个href=\"http://stackoverflow.com/questions/6370412/pthread-exit-in-signal-handler-causes-segmentation-fault\">pthread_exit在信号处理程序引起分段错误)我正在写一个leadlock prevention图书馆,总有一个检查线程做曲线的东西,检查是否存在僵局,这样的话它标志着冲突的一个线程。当该线程捕获信号,它会释放所有的互斥体(ES)它拥有并退出。有多个资源互斥(明显)和一个临界区互斥,所有呼叫收购,释放资源锁和做图的计算必须首先获得该锁。现在有去的问题。随着竞争2(不包括检查线程)的线程,有时的一个线程后程序死锁就会被杀死。在GDB它说死线程拥有临界区锁,但从来没有公布过它。加入破发点中的信号处理程序,并通过步进后,似乎锁属于别人(如预期)了pthread_exit()之前对的,但经过了pthread_exit所有权奇迹般地消失此主题().. 搜索结果我能想到的唯一的猜测是被杀害试图获得临界区锁(因为它想另一个资源互斥体)时挡住的pthread_mutex_lock处线程,则信号传来,打断的pthread_mutex_lock。由于该呼叫不是信号证明,奇怪的事情发生了什么?像信号处理程序可能已返回,该线程得到了锁,然后退出? IDK ..任何洞察力AP preciated!

(This question might be somewhat related to pthread_exit in signal handler causes segmentation fault) I'm writing a leadlock prevention library, where there is always a checking thread doing graph stuff and checks if there is deadlock, if so then it signals one of the conflicting threads. When that thread catches the signal it releases all mutex(es) it owns and exits. There are multiple resource mutexes (obviously) and one critical region mutex, all calls to acquire, release resource lock and do graph calculations must obtain this lock first. Now there goes the problem. With 2 competing (not counting the checking thread) threads, sometimes the program deadlocks after one thread gets killed. In gdb it's saying the dead thread owns critical region lock but never released it. After adding break point in signal handler and stepping through, it appears that lock belongs to someone else (as expected) right before pthread_exit(), but the ownership magically goes to this thread after pthread_exit()..

The only guess I can think of is the thread to be killed was blocking at pthread_mutex_lock when trying to gain the critical region lock (because it wanted another resource mutex), then the signal came, interrupting the pthread_mutex_lock. Since this call is not signal-proof, something weird happened? Like the signal handler might have returned and that thread got the lock then exited? Idk.. Any insight is appreciated!

推荐答案

了pthread_exit 不是异步信号安全的,这样的话你可以从调用它的唯一途径信号处理程序是,如果你保证信号不中断任何非异步信号安全的函数。

pthread_exit is not async-signal-safe, and thus the only way you can call it from a signal handler is if you ensure that the signal is not interrupting any non-async-signal-safe function.

作为一般原则,使用信号作为通信与线程的方法通常是一个非常糟糕的主意。你最终混合两个问题是很难已经足够自己:线程安全和可重入在一个线程中(线程之间的正确同步)

As a general principle, using signals as a method of communication with threads is usually a really bad idea. You end up mixing two issues that are already difficult enough on their own: thread-safety (proper synchronization between threads) and reentrancy within a single thread.

如果有信号你的目标仅仅是指导一个线程终止,一个更好的机制可能是 pthread_cancel可以。为了安全地使用它,但是,将在适当的点被取消必须设置取消处理程序和/或禁用取消暂时当它是不是安全线程(与 pthread_setcancelstate )。此外,要知道,的pthread_mutex_lock 不是取消点。这里的没有安全的办法以中断一个的阻止等待获得一个互斥体,所以如果你需要可中断这样,你可能需要或者更精细的同步设置使用条件变量(condvar等待是可以取消的)一个线程,或者你可以使用,而不是互斥的信号灯。

If your goal with signals is just to instruct a thread to terminate, a better mechanism might be pthread_cancel. To use this safely, however, the thread that will be cancelled must setup cancellation handlers at the proper points and/or disable cancellation temporarily when it's not safe (with pthread_setcancelstate). Also, be aware that pthread_mutex_lock is not a cancellation point. There's no safe way to interrupt a thread that's blocked waiting to obtain a mutex, so if you need interruptability like this, you probably need either a more elaborate synchronization setup with condition variables (condvar waits are cancellable), or you could use semaphores instead of mutexes.

编辑:如果您真的需要一种方法来终止线程​​等待互斥体,你可以替换拨打自己的电话,以的pthread_mutex_lock 函数调用循环 pthread_mutex_timedlock ,并检查每个超时退出的标志。

If you really do need a way to terminate threads waiting for mutexes, you could replace calls to pthread_mutex_lock with calls to your own function that loops calling pthread_mutex_timedlock and checking for an exit flag on each timeout.

这篇关于了pthread_exit()的信号处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆