I have discovered that even a simple wait on QMutex will cause assertion. What am I possibly doing wrong?
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
// ...
}
I get assert and the error message is:
ASSERT:'copy' in the thread\qmutex.cpp, line 525
You need to lock the mutex before calling waitForResponse.wait(). The SyncMgr::onConnected() method should look like this:
void SyncMgr::onConnected()
{
cmdDispatcher.sendGetSerialNo();
mutex.lock();
waitForResponse.wait( &mutex );
// do something
mutex.unlock();
...
}
You can find more information here: http://doc.qt.io/qt-5/qwaitcondition.html#wait
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With