1

I want to use ReadFile() (overlapped) on a named pipe in message mode in combination with an I/O completion port.

So, I have multiple threads waiting for ReadFile() to receive data. The awaken thread will process the message and may call WriteFile() on another pipe handle without overlapped I/O.

I only have real small chunks. Is it possible to use a handle that is associated to an I/O completion port with WriteFile() and non-overlapped I/O?

Are there any issues that I should be aware of?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
xMRi
  • 14,982
  • 3
  • 26
  • 59
  • First you say "may `WriteFile` to another pipe handle" then you ask about using `WriteFile` on a handle that is associated to a completion port. Are you asking about both cases (where the handle is one of the ones associated to a completion port and where it isn't? – Ben Voigt Apr 08 '22 at 18:05
  • Associating a non-overlapped handle with an I/O completion port makes no sense. But there is no problem performing a non-overlapped operation inside a handler for an I/O completion port, it will simply be blocked until the operation is finished. As long as you have other handlers continuing to process the completion port, your overlapped I/O will still be serviced. – Remy Lebeau Apr 08 '22 at 18:53
  • @RemyLebeau Thanks for the answer. All ports where I read and write to are associated to an io completion port. I always use overlapped I/O on reading. And I don't want to use overlapped i/o on writing... that's all. – xMRi Apr 08 '22 at 20:02
  • @xMRi "*I don't want to use overlapped i/o on writing*" - ok, so don't. What is the issue? You are not making much sense. Maybe if you provided a [mcve] demonstrating what you are concerned about, someone can give you a better answer. Are you perhaps trying to WRITE to a handle that is ALSO being used for overlapped READING? – Remy Lebeau Apr 08 '22 at 20:08
  • I am in a design phase. I get a bunch of messages, some must be answered to another Pipe. But I don't want a new message to arrive and handled between receiving and writing. So as long as I start a new Read (overlapped) and I Write the answer (non overlapped) my processes are in a "stable stage". I didn't want to run against a wall, so I asked before... – xMRi Apr 09 '22 at 09:03
  • *Associating a non-overlapped handle with an I/O completion port makes no sense* - this impossible - will be error `STATUS_INVALID_PARAMETER` if try set IOCP on synchronous file object – RbMm Apr 10 '22 at 16:48

1 Answers1

0

Is it possible to use a handle that is associated to an I/O completion port with WriteFile() and non-overlapped I/O?

no, this not possible. if handle associated to an I/O completion - this file handle opened with the FILE_FLAG_OVERLAPPED flag, and as result overlapped (asynchronous) I/O will be used for this file. maximum what you can do - prevent completion port notification. This is done by specifying a valid event handle for the hEvent member of the OVERLAPPED structure, and setting its low-order bit. A valid event handle whose low-order bit is set keeps I/O completion from being queued to the completion port. but I/O anyway will be asynchronous and in this case.

But I don't want a new message to arrive and handled between receiving and writing. So as long as I start a new Read (overlapped) and I Write the answer (non overlapped) my processes are in a "stable stage".

new message not arive until you not start new Read. so when read complete - first decide - are you need answer to message and if yes - do WriteFile - asynchronous - this not create any problem and you not need wait in place until write completed. and then start new Read.

so really you not need synchronous write on file

RbMm
  • 31,280
  • 3
  • 35
  • 56
  • 1
    This confuses me. I use Overlapped I/O and non overlapped I7O always mixed on a file/Pipe. Sample. Read 4 Bytes (length of next message) overlapped. Than read the rest (with length) non overlapped.... For small data messages I can assume that if the first 4 bytes have arrived, the rest is ready tooo. The FILE_FLAG_OVERLAPPED doesn't force me to use ReadFile/WriteFile with a overlapped structure.... – xMRi Apr 11 '22 at 09:12
  • 1
    @xMRi - you fundamental mistaken. if you create file in asynchronous mode (with flag FILE_FLAG_OVERLAPPED ) - all ReadFile/WriteFile will be asynchronous ( or Overlapped - this is the same but i prefer asynchronous term). you can ot mix asynchronous and synchronous I/O on the **same** file. all I/O will be syncronous xor asynchronous - depend from how you open file. *The FILE_FLAG_OVERLAPPED doesn't force me to use ReadFile/WriteFile with a overlapped structure...* - this is **critcal error** - you **must** pass pointer to overlapped structure in case asynchronous file – RbMm Apr 11 '22 at 09:18