pthread_kill 不会杀死线程 C linux
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9345232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pthread_kill doesnt kill thread C linux
提问by randy newfield
i am making a small project which will be incorporated into larger project. basically what it does is keeps track of threads that are created by way of adding them to a main struct which keeps track of what the thread does (its main function) and its pthread_t id. the other struct keeps track of the data to be passed to the function and the element number of where the pthread_t id is stored inside threads[]. its a bit micky mouse and it jumps around a bit but it all works besides when it is time to kill the thread. i get no segfaults and no errors and the program finishes fine, but the thread does not get killed when pthread_kill() is called (the function returns 0 meaning no error and it worked) although the thread continues to run until the main application returns.
我正在制作一个小项目,该项目将被纳入更大的项目中。基本上它的作用是跟踪通过将它们添加到主结构中创建的线程,该主结构跟踪线程的作用(其主要功能)及其 pthread_t id。另一个结构跟踪要传递给函数的数据以及 pthread_t id 存储在线程 [] 中的元素编号。它有点像老鼠,它会跳来跳去,但除了何时终止线程之外,它都可以正常工作。我没有得到段错误和错误,程序完成得很好,但是当 pthread_kill() 被调用时线程没有被杀死(函数返回 0 意味着没有错误并且它工作),尽管线程继续运行直到主应用程序返回。
采纳答案by jilles
pthread_kill()
will not kill a thread. The only difference with kill()
is that the signal is handled by the designated thread and not handled while that thread has the signal masked (see pthread_sigmask()
). A signal like SIGTERM
will by default still terminate the entire process.
pthread_kill()
不会杀死线程。与 的唯一区别kill()
是信号由指定的线程处理,而不是在该线程屏蔽信号时处理(请参阅 参考资料pthread_sigmask()
)。SIGTERM
默认情况下,类似的信号仍将终止整个过程。
If you are considering to call pthread_exit()
from a signal handler, you should probably use pthread_cancel()
instead.
如果您正在考虑pthread_exit()
从信号处理程序调用,您可能应该pthread_cancel()
改用。
Cancellation is safe if all code that may be cancelled cooperates (or the code that calls it disables cancellation for the time). Most libraries do not care about this, though.
如果所有可能被取消的代码合作(或调用它的代码暂时禁用取消),则取消是安全的。不过,大多数图书馆并不关心这一点。
A safer method is to ask the thread to exit without any force, such as by sending a special message to it (if the thread normally processes messages).
一种更安全的方法是要求线程在没有任何强制的情况下退出,例如通过向它发送特殊消息(如果线程正常处理消息)。
Alternatively, don't bother to kill any threads and just call _exit()
, _Exit()
or quick_exit()
.
或者,不要费心杀死任何线程,只需调用_exit()
,_Exit()
或quick_exit()
。
回答by NothingMore
From http://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_kill.html
来自http://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_kill.html
As in kill(), if sig is zero, error checking is performed but no signal is actually sent.
与 kill() 中一样,如果 sig 为零,则执行错误检查但实际上不发送任何信号。
so the following
所以以下
pthread_kill(threads[i].tID, 0);
pthread_kill(线程[i].tID,0);
Wont actually kill the thread. You need to use an actual signal to kill a thread. A list of signals can be found here:
实际上不会杀死线程。您需要使用实际信号来终止线程。可以在此处找到信号列表:
http://pubs.opengroup.org/onlinepubs/7908799/xsh/signal.h.html
http://pubs.opengroup.org/onlinepubs/7908799/xsh/signal.h.html