windows WaitForSingleObject 在文件句柄上?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/775014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:20:03  来源:igfitidea点击:

WaitForSingleObject on a file handle?

windowsmsvcrt

提问by joshk0

What happens when you call WaitForSingleObject()on a handle you've created with CreateFile()or _get_osfhandle()?

当你调用WaitForSingleObject()你用CreateFile()or创建的句柄时会发生什么_get_osfhandle()

For reasons not worth explaining I would like to use WaitForSingleObject()to wait on a HANDLE that I've created with _get_osfhandle(fd), where fdcomes from a regular call to _open(). Is this possible?

至于原因,不值得说明的是我想用WaitForSingleObject()伺候,我已经与创建句柄_get_osfhandle(fd),其中fd从常规调用来_open()。这可能吗?

I have tried it in practice, and on some machines it works as expected (the HANDLE is always in the signaled state because you can read more data from it), and on some machines WaitForSingleObject()will block indefinitely if you let it.

我已经在实践中尝试过,在某些机器上它按预期工作(HANDLE 始终处于信号状态,因为您可以从中读取更多数据),并且在某些机器上,WaitForSingleObject()如果您让它无限期地阻塞。

The MSDN page for WaitForSingleObject()says that the only supported things that it handles are "change notifications, console input, events, memory resource notifications, mutex, processes, semaphores, threads, and waitable timers."

MSDN 页面WaitForSingleObject()说,它处理的唯一支持的事情是“更改通知、控制台输入、事件、内存资源通知、互斥锁、进程、信号量、线程和可等待计时器。”

Additionally, would it be different if I used CreateFile()instead of _get_osfhandle()on a CRT file descriptor?

另外,如果我在 CRT 文件描述符上使用CreateFile()而不是使用它会有所不同_get_osfhandle()吗?

采纳答案by MSN

Don't do it. As you can see, it has undefined behavior.

不要这样做。如您所见,它具有未定义的行为。

Even when the behavior is defined, it's defined in such a way as to be relatively not useful unless you don't like writing additional code. It is signaled when any asynchronous I/O operation on that handle completes, which does not generalize to tracking which I/O operation finished.

即使定义了行为,它的定义方式也相对没有用,除非您不喜欢编写额外的代码。当该句柄上的任何异步 I/O 操作完成时会发出信号,这不会概括为跟踪哪个 I/O 操作完成。

Why are you trying to wait on a file handle? Clearly the intent matters when you are doing something that isn't even supported well enough to not block indefinitely.

为什么要等待文件句柄?显然,当你做的事情甚至没有得到足够好的支持而不会无限期地阻止时,意图很重要。

回答by 1800 INFORMATION

I found the following links. The concensus seems to me, don't do it.

我找到了以下链接。共识在我看来,不要这样做。

Waiting on a file handle

When an I/O operation is started on an asynchronous handle, the handle goes into a non-signaled state. Therefore, when used in the context of a WaitForSingleObject or WaitForMultipleObjects operation, the file handle will become signaled when the I/O operation completes. However, Microsoft actively discourages this technique; it does not generalize if there exists more than one pending I/O operation; the handle would become signaled if any I/O operation completed. Therefore, although this technique is feasible, it is not considered best practice.

等待文件句柄

在异步句柄上启动 I/O 操作时,句柄进入无信号状态。因此,当在 WaitForSingleObject 或 WaitForMultipleObjects 操作的上下文中使用时,文件句柄将在 I/O 操作完成时发出信号。然而,微软积极反对这种技术;如果存在多个待处理的 I/O 操作,则它不会一概而论;如果任何 I/O 操作完成,句柄将发出信号。因此,虽然这种技术是可行的,但它不被认为是最佳实践。

Use ReadDirectoryChangesW in overlapped mode. WaitForSingleObject can wait on the event in the OVERLAPPED struct.

You can also use the API WaitForSingleObject() to wait on a file change if you use the following change notification function:
FindFirstChangeNotification()
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/findfirstchangenotification.asphttp://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/waitforsingleobject.asp

An interesting note on "evilness" of ReadDirectoryChangesW:
http://blogs.msdn.com/ericgu/archive/2005/10/07/478396.aspx

在重叠模式下使用 ReadDirectoryChangesW。WaitForSingleObject 可以等待 OVERLAPPED 结构中的事件。

如果您使用以下更改通知函数,您还可以使用 API WaitForSingleObject() 等待文件更改:
FindFirstChangeNotification()
http://msdn.microsoft.com/library/default.asp?url=/library/en- us/fileio/fs/findfirstchangenotification.asp http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/waitforsingleobject.asp

关于 ReadDirectoryChangesW 的“邪恶”的有趣注释:http:
//blogs.msdn.com/ericgu/archive/2005/10/07/478396.aspx