在下面的代码中,我得到一个 ASSERT in unlock ()
,因为lockingThreads (a QList<QThread*>
) 不包含解锁(当前)线程。检查lockingThreads,我发现一个“ntdll”线程,我不知道它做了什么,只能访问不提供有用信息的反汇编。这个问题很难重现,所以感谢任何提示(可能是这个逻辑中的一个愚蠢的转折)。
编辑:互斥锁是递归的。
MyMutex::MyMutex (QMutex::RecursionMode mode)
: QMutex (mode)
, lockCount (0)
{
}
MyMutex::~MyMutex ()
{
ASSERT (lockingThreads.isEmpty ());
}
void MyMutex::lock ()
{
lockCount++;
#if MYDEBUG
// grab thread before lock so we can identify it if it deadlocks here
QThread* thread = QThread::currentThread ();
if (!isRecursive ())
{
ASSERT (!lockingThreads.contains (thread));
if (!lockingThreads.contains (thread))
lockingThreads += thread;
}
else
lockingThreads += thread;
#endif
QMutex::lock ();
}
void MyMutex::unlock ()
{
#if MYDEBUG
QThread* thread = QThread::currentThread ();
ASSERT (lockingThreads.contains (thread));
lockingThreads.removeOne (thread);
if (lockingThreads.contains (thread) && !isRecursive ())
ASSERTFALSE;
#endif
QMutex::unlock ();
lockCount--;
}