How can I cancel a blocking call such as select() or read()?

青春壹個敷衍的年華 提交于 2019-12-24 00:38:12

问题


As the title says, how can I cancel a blocking call such as select() or (in the case of serial comm and similar) read() from another thread? What is the traditional way of solving things like this? I suppose one could use a small timeout and that would probably work fine but that seems like a bit of a hack to me.


回答1:


The traditional way of interrupting select(2) is the self-pipe trick. The input/output calls are better served by having the descriptors non-blocking and handling EAGAIN.




回答2:


In these situations you should probably use a small or immediate timeout and have additional logic that handles when/how/why the next select/read attempt is made.



来源:https://stackoverflow.com/questions/7016977/how-can-i-cancel-a-blocking-call-such-as-select-or-read

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