Groups | Blog | Home
all groups > visual c > december 2003 >

visual c : When can WaitForSingleObject return WAIT_FAILED?


Sean Kelly
12/31/2003 1:57:57 PM
When both waiting on an event and a simple unnamed mutex, I'm wondering
when WaitForSingleObject might return WAIT_FAILED. These are both
execution paths I'd very much like to avoid exceptional behavior but I
haven't found much documentation on the specifics. Can anyone offer advice?

Sean
William DePalo
1/1/2004 7:45:26 PM
[quoted text, click to view]
advice?

Well, suppose you pass an invalid handle to the function? I'd expect that
you will get a WAIT_FAILED status and then INVALID_HANDLE in the call to
GetLastError(). I don't think there is any way you can avoid checking the
status of the code _somewhere_.

Regards,
Will

Sean Kelly
1/2/2004 2:34:51 PM
[quoted text, click to view]

This is in an object and ensapsulation ensures that the parameters
passed will always be valid. Basically, I'm trying to avoid the
possibility that an exception may be thrown when acquiring a lock on a
mutex. Critical section operations are guranteed to succeed for this
reason. More info: it's an unnamed mutex without any security
restrictions. Basically, just an object that creates the mutex on
construction, locks/unlocks the mutex when requested, and destroys the
mutex on destruction. What I'm looking for is someone with enough
knowledge of the inner workings of the call that they could tell be what
conditions might cause a WAIT_FAILED in this instance. I'm hoping that
if I can satisfy enough preconditions before calling the function I can
be guranteed that the function will never fail in this way.

Sean
Carl Daniel [VC++ MVP]
1/2/2004 7:11:28 PM
[quoted text, click to view]

If you're waiting on a mutex, you'll normally get WAIT_FAILED for only two
reasons: 1. The HANDLE you passed in is invalid. 2. Another thread that
owned the mutex was terminated without releasing ownership.

There's no guarantee that those are the only two circumstances though.
There are probably other, extremely rare and esoteric failures possible.

-cd

William DePalo [MVP VC++]
1/2/2004 7:45:16 PM
[quoted text, click to view]

In an environment where "naked" pointers are used, memory corruption is not
impossible, encapsulation be damned. It is hard to imagine a Win32 program
without a single naked pointer.

[quoted text, click to view]

Then I suggest that you post again in the kernel group. Perhaps there you'll
run into someone who has seen the source or reverse engineered it.

[quoted text, click to view]

Good luck.

Regards,
Will

Sean Kelly
1/3/2004 12:14:04 AM
[quoted text, click to view]

From what I read in MSDN I thought that case 2 would return
WAIT_ABANDONED and mutex ownership would be transferred.

[quoted text, click to view]

That's what I'm afraid of. Worst case I suppose I can just always use
critical sections to avoid the possibility of failure.

Sean
Carl Daniel [VC++ MVP]
1/3/2004 9:04:20 AM
[quoted text, click to view]

You're right about that.

-cd

AddThis Social Bookmark Button