gpt4 book ai didi

windows - 如何找出 CancelIo() 何时完成?

转载 作者:可可西里 更新时间:2023-11-01 11:39:27 25 4
gpt4 key购买 nike

CancelIo()应该取消与调用线程关联的所有未决 I/O 操作。根据我的经验,CancelIo() 有时也会取消 future I/O 操作。鉴于:

ReadFile(port, buffer, length, &bytesTransferred, overlapped);
  1. 如果我在读取之前调用 CancelIo(port)GetQueuedCompletionStatus() 将永远阻塞,永远不会接收读取操作。
  2. 如果我在读取后立即调用 CancelIo(port)GetQueuedCompletionStatus() 将返回 0 和 GetLastError()==ERROR_OPERATION_ABORTED
  3. 如果我调用 CancelIo(port) 并且没有挂起或后续读取,GetQueuedCompletionStatus() 将永远阻塞。

这里的关键点是无法检测 CancelIo() 何时完成执行。我如何确保 CancelIo() 已完成执行并且可以安全地发出进一步的读取请求?

PS:看http://osdir.com/ml/lib.boost.asio.user/2008-02/msg00074.htmlhttp://www.boost.org/doc/libs/1_44_0/doc/html/boost_asio/using.html听起来 CancelIo() 并不是真的可用。必须客户要求 Windows XP 支持。我有哪些选择?

注意:我正在从串口读取数据。

最佳答案

CancelIo() 工作正常。我误解了我的代码。

经过进一步调查,发现代码正在调用 CancelIo(),然后调用 ReadFile(),超时 INFINITE。完成端口从未收到读取通知,因为远程端从未发送任何内容。也就是说,CancelIo()并没有取消后续的操作。

我发现了一些令人大开眼界的文档 here :

Be careful when coding for asynchronous I/O because the system reserves the right to make an operation synchronous if it needs to. Therefore, it is best if you write the program to correctly handle an I/O operation that may be completed either synchronously or asynchronously. The sample code demonstrates this consideration.

事实证明,如果正在读取的数据已被设备驱动程序缓存,则设备驱动程序可能会选择以同步方式处理异步操作。经过进一步调查,我发现当 ReadFile() 之前调用 CancelIo() 时,有时会导致后者同步返回。我不知道为什么在 CancelIo() 之后完成端口从未收到 ReadFile() 的通知,但我无法再重现此问题。

无论 ReadFile() 是同步还是异步,完成端口都会发出信号。

关于windows - 如何找出 CancelIo() 何时完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3921111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com