在Linux上的线程中中断系统调用 [英] Interrupting syscalls in threads on linux

查看:313
本文介绍了在Linux上的线程中中断系统调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个pthread循环运行,以阻塞方式调用accept().有什么办法可以中断另一个线程的调用吗?一切都指向向线程发送信号,但是显然您只能向进程发送信号.

I have a pthread that runs in a loop, calling accept() in a blocking manner. Is there any way to interrupt that call from another thread? Everything points to sending the thread a signal, but apparently you can only send a process a signal.

我不能仅仅杀死线程,因为这会使套接字保持打开状态.而且那也不是很干净.真的没有办法做到这一点吗?

I can't just kill the thread because then it leaves the socket open. And that's not very clean anyway. Is there really no way to do this?

推荐答案

您可以使用

You can signal a thread using pthread_kill(3).

pthread_kill()函数将信号sig发送到线程,另一个 线程与调用方的进程相同.

The pthread_kill() function sends the signal sig to thread, another thread in the same process as the caller.

如果安装了信号处理程序,则该处理程序将在 线程线程.

If a signal handler is installed, the handler will be invoked in the thread thread.

请注意,您不必杀死线程;您可以发送一个使accept失败并导致EINTR失败的信号.

Note, you don't have to kill the thread; you can send a signal that simply makes accept fail with EINTR.

这篇关于在Linux上的线程中中断系统调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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