Interrupting syscalls in threads on linux

跟風遠走 提交于 2019-12-11 01:38:57

问题


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?


回答1:


You can signal a thread using pthread_kill(3).

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.

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




回答2:


Either use select(), or send the singal to the process (this will be a problem if you just want to interupt one of the threads).



来源:https://stackoverflow.com/questions/6910335/interrupting-syscalls-in-threads-on-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!