我发现,即使是对QMutex的简单等待也会导致断言。我到底做错什么了?
QMutex mutex;
SyncMgr::SyncMgr(QObject *parent) : QObject(parent)
{
moveToThread( &thread );
thread.start();
process = new QProcess( this);
connect( process, SIGNAL(readyReadStandardOutput()), this, SLOT(onReadyReadStandardOutput() ) );
connect( process, SIGNAL(readyReadStandardError()), this, SLOT(onReadyReadStandardError() ) );
}
SyncMgr::~SyncMgr()
{
delete process;
}
void SyncMgr::onConnected()
{
cmdDispatcher.sendGetSerialNo();
// this asserts
waitForResponse.wait( &mutex ); // waitForResponse is CWaitCondition object
// ...
}我得到断言,错误消息是:
断言: thread\qmutex.cpp中的“复制”,第525行
发布于 2016-12-26 14:16:42
在调用waitForResponse.wait()之前,您需要锁定互斥锁。SyncMgr::onConnected()方法应该如下所示:
void SyncMgr::onConnected()
{
cmdDispatcher.sendGetSerialNo();
mutex.lock();
waitForResponse.wait( &mutex );
// do something
mutex.unlock();
...
}您可以在这里找到更多信息:http://doc.qt.io/qt-5/qwaitcondition.html#wait
https://stackoverflow.com/questions/30986719
复制相似问题