Menu

#34 Interruptible blocking read

open
None
5
2014-08-15
2013-12-07
No

My application has a read loop that is constantly polling the port because it is also checking if an event is set that tells it to stop reading from the port. The problem is that this uses 100% of a CPU. I added a timeout but it can't be too long because the request to stop comes from the user via the UI. Now it is using less CPU but it could be using much less if I could use a blocking read.

If read took an event such that if the event is set then the read is interrupted, maybe throws an interrupted exception. This can be implemented on posix by adding a self-pipe to the select call. On windows it can be done similarly by waiting on multiple objects, the second of which is our event.

If you agree with this idea then I'd be happy to send a patch implementing this.

If you have a better solution then I'd be even happier.

Discussion

  • Chris Liechti

    Chris Liechti - 2013-12-09

    hm, i would not mind seeing an abortRead() function if it worked on all platforms. (patches welcome)

    it would be preferable if an abortRead call would also work under normal operation (not only for scenarios where the application is about to exit), leaving the port usable.

    it seems that on windows it must be a CancelIoEx call. waiting on multiple objects would not be useful, as the actual read operation would not be aborted and further calls to read may fail.

     
  • Chris Liechti

    Chris Liechti - 2013-12-09
    • assigned_to: Chris Liechti
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.