WaitForSingleObject函数的原型如下:
DWORD WINAPI WaitForSingleObject(
__in HANDLE hHandle,
__in DWORD dwMilliseconds
);
关于WAIT_ABANDONED返回值,MSDN的解释如下:
The specified object is a mutex object that was not released by the thread that owned the mutex object before the owning thread terminated. Ownership of the mutex object is granted to the calling thread, and the mutex is set to nonsignaled. If the mutex was protecting persistent state information, you should check it for consistency. |
实际就是下面这中情况:
假设有A、B两个线程和一个互斥量hMutex。如果A线程调用WaitForSingleObject获取到互斥量后,并没有调用ReleaseMutex来释放互斥量就终止了(如调用了ExitThread,TerminateThread)。然后线程B调用WaitForSingleObject就会返回WAIT_ABANDONED,并且线程B获取到互斥量,线程B使用完成后应该调用ReleasMutex释放互斥量。
当返回WAIT_ABANDONED时,应该检查一下互斥量所保护的资源的一致性。