What happens when you call WaitForSingleObject()
on a handle you've created with 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 fd
comes from a regular call to _open()
. Is this possible?
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.
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."
Additionally, would it be different if I used CreateFile()
instead of _get_osfhandle()
on a CRT file descriptor?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With