windows 为什么 WaitForSingleObject 会返回 WAIT_FAILED

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/210751/
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 11:28:47  来源:igfitidea点击:

Why would WaitForSingleObject return WAIT_FAILED

windowswinapimutex

提问by mat_geek

MSDN says

MSDN 说

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

如果函数失败,则返回值为 WAIT_FAILED。要获取扩展错误信息,请调用 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?

但可能发生的原因是什么?

回答by 1800 INFORMATION

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

如果您没有对象的 SYNCHRONIZE 特权,那么您不能等待。WAIT_FAILED 将被返回。

回答by mat_geek

Passing in a bogus object might cause that.

传入一个伪造的对象可能会导致这种情况。

回答by Windows programmer

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

在等待句柄时关闭句柄也会导致未定义的行为。

回答by osullivj

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://blogs.msdn.microsoft.com/oldnewthing/20141015-00/?p=43843

当传入一组线程句柄时,我从 WaitForMultipleObjects 得到 WAIT_FAILED,因为其中一个是伪句柄。一如既往,不朽的陈雷蒙解释,并提供了修复:https: //blogs.msdn.microsoft.com/oldnewthing/20141015-00/?p=43843