Is it necessary to set hEvent on the OVERLAPPED structure when doing I/O completion ports?
I'm using I/O completion ports on Windows for serial port communication (we will potentially have lots and lots of serial port usage). I've done the usual, creating the IOCP, spinning up the I/O threads, and associating my CreateFile()
handle with the IOCP (CreateFile()
was called with FILE_FLAG_OVERLAPPED
). That's all working fine. I've set the COMMTIMEOUTS
all to 0 except ReadIntervalTimeout
which is set to MAXDWORD
in order to be completely async.
In my I/O thread, I've noticed that GetQueuedCompletionStatus()
blocks indefinitely. I'm using an INFINITE
timeout. So I put a ReadFile()
call right after I associate my handle with the IOCP. Now that causes GetQueuedCompletionStatus()
to release immediately for some reason with 0 bytes transferred, but there's no errors (it returns true, GetLastError() reports 0). I obviously want it to block if there's nothing for it to do. If I put another ReadFile()
after GetQueuedCompletionStatus(), then another thread in the pool will pick it up with 0 bytes transferred and no errors.
In the examples I've seen and followed, I don't see anyone setting the hEvent
on the OVERLAPPED
structure when using IOCP. Is that necessary? I don't care to ever block IOCP threads -- so I'll never be interested in CreateEvent(...) | 1
.
If it's not necessary, what could be causing the problem? GetQueuedCompletionStatus()
needs to block until data arrives on the serial port.
Are there any good IOCP serial port examples out there? I haven't found a complete serial port + IOCP example out there. Most of them are for sockets. In the开发者_开发问答ory, it should work for serial ports, files, sockets, etc.
I figured it out -- I wasn't calling SetCommMask()
with EV_RXCHAR | EV_TXEMPTY
and then WaitCommEvent()
with the OVERLAPPED
struct. After I did that, my IOCP threads behaved as expected. GetQueuedCompletionStatus()
returned when a new character appeared on the port. I could then call ReadFile()
.
So to answer the original question: "no, you don't need to set hEvent for IOCP with serial ports."
精彩评论