在linux上中止阻塞读取 [英] aborting a blocking read on linux

查看:177
本文介绍了在linux上中止阻塞读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个线程中运行的linux中有一个阻塞读取。

I have a blocking read in linux running in a thread.

在程序关闭期间,我想打破这个读取的线程。不幸的是,我无法使用轮询或选择并编写正确的代码,因为从中读取的文件是不实现轮询/选择功能的设备驱动程序。

During program shutdown I want to break the thread out of this read. Unfortunately I can't use poll or select and write proper code because the file that is read from is a device driver that does not implement poll/select functionality.

作为临时解决方案我目前通过pthread_kill向线程发送SIGUSR1信号并从处理程序调用pthread_exit。这会杀死线程并且到目前为止工作,但我对解决方案不满意,因为信号可能发生在任何地方,而不仅仅是在读取内。

As a temporary solution I currently send a SIGUSR1 signal via pthread_kill to the thread and call pthread_exit from the handler. This kills the thread and works so far, but I'm not satisfied with the solution because the signal could occur anywhere, not only within the read.

是否有任何机制在Linux上中断阻塞读取?

Is there any mechanism to interrupt a blocking read on linux?

顺便说一句 - 我试图从另一个线程关闭文件句柄,希望这会产生一些IO错误。不幸的是,这个简单的解决方案根本不起作用。

Btw - I tried closing the file-handle from a different thread in hope that this would give a IO-error of some sorts. Unfortunately this simple solution didn't worked at all.

推荐答案

如果您只想让信号影响读取,请使用 pthread_sigmask() 到保持信号阻塞,直到读取之前,然后再次阻止它。

If you only want the signal to affect the read, use pthread_sigmask() to keep the signal blocked until just before the read and to block it again afterwards.

这篇关于在linux上中止阻塞读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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