pthread_kill 不会杀死线程 C linux [英] pthread_kill doesnt kill thread C linux

查看:333
本文介绍了pthread_kill 不会杀死线程 C linux的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个小项目,该项目将被合并到更大的项目中.基本上它的作用是跟踪通过将它们添加到主结构中创建的线程,该主结构跟踪线程的作用(其主要功能)及其 pthread_t id.另一个结构跟踪要传递给函数的数据以及 pthread_t id 存储在线程 [] 中的元素编号.它有点像老鼠,它会跳来跳去,但除了何时终止线程之外,它都可以正常工作.我没有得到段错误,也没有错误,程序完成得很好,但是当 pthread_kill() 被调用时线程没有被杀死(函数返回 0 意味着没有错误并且它工作),尽管线程继续运行直到主应用程序返回.

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_kill() 不会杀死线程.与 kill() 的唯一区别是信号由指定的线程处理,而不是在该线程屏蔽信号时处理(参见 pthread_sigmask()).像 SIGTERM 这样的信号默认仍会终止整个进程.

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_exit(),您可能应该改用 pthread_cancel().

If you are considering to call pthread_exit() from a signal handler, you should probably use pthread_cancel() instead.

如果所有可能被取消的代码合作(或调用它的代码暂时禁用取消),则取消是安全的.不过,大多数图书馆并不关心这一点.

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).

或者,不要费心杀死任何线程,只需调用 _exit()_Exit()quick_exit().

Alternatively, don't bother to kill any threads and just call _exit(), _Exit() or quick_exit().

这篇关于pthread_kill 不会杀死线程 C linux的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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