做I/O完成端口时是否需要在OVERLAPPED结构上设置hEvent?
我在 Windows 上使用 I/O 完成端口进行串行端口通信(我们可能会大量使用串行端口)。我已经完成了通常的操作,创建 IOCP,启动 I/O 线程,并将我的 CreateFile()
句柄与 IOCP 关联起来(CreateFile()
是通过以下方式调用的FILE_FLAG_OVERLAPPED
)。这一切都工作正常。我已将 COMMTIMEOUTS
全部设置为 0,但 ReadIntervalTimeout
除外,它设置为 MAXDWORD
以实现完全异步。
在我的 I/O 线程中,我注意到 GetQueuedCompletionStatus()
无限期地阻塞。我正在使用 INFINITE
超时。因此,在将句柄与 IOCP 关联后,我立即调用了 ReadFile()
。现在,这会导致 GetQueuedCompletionStatus()
由于某种原因立即释放并传输 0 个字节,但没有错误(它返回 true,GetLastError() 报告 0)。如果没有什么可做的,我显然希望它被阻止。如果我在 GetQueuedCompletionStatus() 之后放置另一个 ReadFile()
,那么池中的另一个线程将拾取它并传输 0 字节并且没有错误。
在我看到和遵循的示例中,我没有看到任何人在使用 IOCP 时在 OVERLAPPED
结构上设置 hEvent
。有必要吗?我不关心阻塞 IOCP 线程——所以我永远不会对 CreateEvent(...) | 感兴趣。 1..
如果没有必要,可能是什么原因造成的? GetQueuedCompletionStatus()
需要阻塞,直到数据到达串行端口。
有没有好的 IOCP 串口示例?我还没有找到完整的串行端口+ IOCP 示例。大多数都是用于套接字的。理论上,它应该适用于串行端口、文件、套接字等。
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 theory, it should work for serial ports, files, sockets, etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现了——我没有用
EV_RXCHAR | 调用
,然后使用SetCommMask()
EV_TXEMPTYOVERLAPPED
结构的WaitCommEvent()
。完成此操作后,我的 IOCP 线程的行为符合预期。当端口上出现新字符时,GetQueuedCompletionStatus()
返回。然后我可以调用ReadFile()
。所以回答原来的问题:“不,你不需要为串口的IOCP设置hEvent。”
I figured it out -- I wasn't calling
SetCommMask()
withEV_RXCHAR | EV_TXEMPTY
and thenWaitCommEvent()
with theOVERLAPPED
struct. After I did that, my IOCP threads behaved as expected.GetQueuedCompletionStatus()
returned when a new character appeared on the port. I could then callReadFile()
.So to answer the original question: "no, you don't need to set hEvent for IOCP with serial ports."