If I define a template struct like so:
template <typename T>
struct SYNCHED_DATA
{
SYNCHED_DATA()
{
hMutex = ::CreateMutex(NULL, FALSE, NULL);
}
~SYNCHED_DATA()
{
if(hMutex)
CloseHandle(hMutex);
hMutex = NULL;
}
void set(T* pV)
{
if(pV)
{
::WaitForSingleObject(hMutex, INFINITE);
var = *pV;
::ReleaseMutex(hMutex);
}
}
void get(T* pV)
{
if(pV)
{
::WaitForSingleObject(hMutex, INFINITE);
*pV = var;
::ReleaseMutex(hMutex);
}
}
private:
HANDLE hMutex;
T var;
SYNCHED_DATA(const SYNCHED_DATA& s)
{
}
SYNCHED_DATA& operator = (const SYNCHED_DATA& s)
{
}
};
Can I be assured that those WaitForSingleObject() APIs will always return WAIT_OBJECT_0? And if no, in what circumstances can they fail and how am I supposed to handle it then?
The Old New Thing has an article on how closing the handle before the wait succeeded will result in
WAIT_ABANDONEDfor anybody waiting:http://blogs.msdn.com/b/oldnewthing/archive/2005/09/12/463977.aspx
There is some discussion of
WAIT_FAILEDon this very site:Why would WaitForSingleObject return WAIT_FAILED
These suggest that failure is a possibility even with an infinite timeout.