开发者

Interrupting syscalls in threads on linux

开发者 https://www.devze.com 2023-03-25 19:18 出处:网络
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 apparent

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 j开发者_Go百科ust 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).

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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号