mat_geek
mat_geek

Reputation: 2511

Why would WaitForSingleObject return WAIT_FAILED

MSDN says

If the function fails, the return value is WAIT_FAILED. To get extended error information, call GetLastError.

The code is:

HANDLE     m_mutex_handle;    /**< m_mutex_handle. The handle to the created mutex. */
m_mutex_handle = ::CreateMutex( 0, false, NULL );
::WaitForSingleObject( m_mutex_handle, INFINITE );

But what are the reasons that could happen?

Upvotes: 6

Views: 11785

Answers (4)

osullivj
osullivj

Reputation: 351

I got WAIT_FAILED from WaitForMultipleObjects when passing in an array of thread handles as one of them was a pseudo handle. As ever the immortal Raymond Chen explained, and provided the fix: https://devblogs.microsoft.com/oldnewthing/20141015-00/?p=43843

Upvotes: 1

1800 INFORMATION
1800 INFORMATION

Reputation: 135285

If you lack the SYNCHRONIZE privilege on the object, then you cannot wait. WAIT_FAILED will be returned.

Upvotes: 7

Windows programmer
Windows programmer

Reputation: 8065

Closing a handle while the handle is being waited on can also cause undefined behaviour.

Upvotes: 1

Mike F
Mike F

Reputation:

Passing in a bogus object might cause that.

Upvotes: 4

Related Questions