0

I've inherited some code which, to initialise some hardware, writes a few bytes and then waits for a return. To do this it calls the _write function from io.h. From my testing, it looks like it's locking up at that point. So my questions are as follows:

  1. Is that function a blocking function?
  2. Is there a way of setting a timeout?
  3. Is there an alternative non-blocking function I could use instead?
Jon Cage
  • 36,366
  • 38
  • 137
  • 215
  • 1
    The standard defines no `_write` in io.h. Perhaps you should use the stream facilities instead, which will buffer things for you? Oh, and I believe the answers are yes, no, and http://msdn.microsoft.com/en-us/library/aa365683.aspx – Billy ONeal Sep 09 '10 at 14:14

1 Answers1

1

If you want to do async I/O on Windows then either use the Win32 APIs directly (look at docs for WriteFileEx/ReadFileEx, which contain pointers to general background on async I/O vs sync) or consider boost::asio.

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
  • I don't want async I/O I just need it to time out if there's a problem. – Jon Cage Sep 10 '10 at 07:50
  • I guess I could write a callback function which sets a flag when it's done, and a wrapper to wait until that flag is set or timed out.. – Jon Cage Sep 10 '10 at 07:53
  • That's the kind of thing you have to consider - remembering to correlate and discard pending callbacks after the timeout. There's no way to even approach this problem with sync I/O as a baseline though. I/O completion ports is the most efficient way to do async I/O on Windows. I gather boost::asio is good but have not used it myself. – Steve Townsend Sep 10 '10 at 11:06