我们有互斥对象的包装。这个包装器的构造函数创建了一个互斥锁。它还有Enter和Leave函数,它们基本上调用WaitForSingleObject和ReleaseMutex。
现在程序线程正在执行多个功能,如
void A()
{
mutexObj->Enter();
B();
mutexObj->Leave();
}
void B()
{
mutexObj->Enter();
C();
mutexObj->Leave();
}
我看到线程在B()函数处被停止。我读过的关于互斥的内容是我们可以从同一个线程多次调用WaitforSingleObjects,然后我的代码被阻止了。
这是互斥的实现
CMutex::CMutex(const TCHAR *name)
: m_hMutex(CreateMutex(NULL, FALSE, name))
{
}
CMutex::~CMutex()
{
if (m_hMutex != NULL)
{
CloseHandle(m_hMutex);
m_hMutex= NULL;
}
}
bool Enter(DWORD milli= INFINITE){
HANDLE handles[1]= { m_hMutex };
return(MsgWaitForMultipleObjects(1,handles,FALSE, milli,QS_ALLINPUT) == WAIT_OBJECT_0);
}
void Leave(){ReleaseMutex(m_hMutex);}
答案 0 :(得分:2)
是的,可以在同一个线程中多次等待互斥锁。 documentation同样说:
在线程获得互斥锁的所有权后,它可以在重复调用wait-functions时指定相同的互斥锁,而不会阻止其执行。这可以防止线程在等待它已拥有的互斥锁时自行死锁。要在这种情况下释放其所有权,每次互斥锁满足等待函数的条件时,线程必须调用一次ReleaseMutex。
话虽如此,您的代码并不安全,因为您忽略了等待的结果,因此在访问受互斥锁保护的内容之前,您并未确定实际拥有互斥锁。
请改为尝试:
class CMutex
{
public:
CMutex(const TCHAR *name = NULL);
~CMutex();
bool Enter(DWORD milli = INFINITE);
void Leave();
};
CMutex::CMutex(const TCHAR *name)
: m_hMutex(CreateMutex(NULL, FALSE, name))
{
}
CMutex::~CMutex()
{
if (m_hMutex != NULL)
{
CloseHandle(m_hMutex);
m_hMutex = NULL;
}
}
bool CMutex::Enter(DWORD milli)
{
return (WaitForSingleObject(m_hMutex, milli) == WAIT_OBJECT_0);
}
void CMutex::Leave()
{
ReleaseMutex(m_hMutex);
}
void A()
{
if (mutexObj->Enter()) // <-- CHECK THE RESULT!!!
{
B();
mutexObj->Leave();
}
}
void B()
{
if (mutexObj->Enter()) // <-- CHECK THE RESULT!!!
{
C();
mutexObj->Leave();
}
}
我会更进一步使用RAII来管理锁:
class CMutex
{
public:
CMutex(const TCHAR *name = NULL);
~CMutex();
bool Enter(DWORD milli = INFINITE);
void Leave();
class Lock
{
private:
CMutex &m_mutex;
bool m_locked;
public:
Lock(CMutex &mutex);
~Lock();
bool isLocked() const;
};
};
CMutex::CMutex(const TCHAR *name)
: m_hMutex(CreateMutex(NULL, FALSE, name))
{
}
CMutex::~CMutex()
{
if (m_hMutex != NULL)
{
CloseHandle(m_hMutex);
m_hMutex = NULL;
}
}
bool Enter(DWORD milli)
{
return (WaitForSingleObject(m_hMutex, milli) == WAIT_OBJECT_0);
}
void Leave()
{
ReleaseMutex(m_hMutex);
}
CMutex::Lock::Lock(CMutex &mutex)
: m_mutex(mutex), m_locked(mutex.Enter())
{
}
CMutex::Lock::~Lock()
{
if (m_locked)
{
m_mutex.Leave();
m_locked = false;
}
}
CMutex::Lock::isLocked() const
{
return m_locked;
}
void A()
{
CMutex::Lock lock(*mutexObj);
if (lock.isLocked()) // <-- CHECK THE RESULT!!!
B();
}
void B()
{
CMutex::Lock lock(*mutexObj);
if (lock.isLocked()) // <-- CHECK THE RESULT!!!
C();
}
甚至:
class CMutex
{
public:
CMutex(const TCHAR *name = NULL);
~CMutex();
bool Enter(DWORD milli = INFINITE);
void Leave();
class Lock
{
private:
CMutex &m_mutex;
public:
Lock(CMutex &mutex);
~Lock();
};
};
CMutex::CMutex(const TCHAR *name)
: m_hMutex(CreateMutex(NULL, FALSE, name))
{
if (!m_hMutex)
throw std::runtime_error("cannot create the mutex handle");
}
CMutex::~CMutex()
{
CloseHandle(m_hMutex);
m_hMutex = NULL;
}
bool Enter(DWORD milli)
{
return (WaitForSingleObject(m_hMutex, milli) == WAIT_OBJECT_0);
}
void Leave()
{
ReleaseMutex(m_hMutex);
}
CMutex::Lock::Lock(CMutex &mutex)
: m_mutex(mutex)
{
if (!m_mutex.Enter())
throw std::runtime_error("cannot lock the mutex");
}
CMutex::Lock::~Lock()
{
m_mutex.Leave();
}
void A()
{
CMutex::Lock lock(*mutexObj);
B();
}
void B()
{
CMutex::Lock lock(*mutexObj);
C();
}