Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Waiting on QMutex asserts

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

like image 245
zar Avatar asked Jun 22 '15 17:06

zar


1 Answers

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

like image 117
ramzes2 Avatar answered Oct 11 '22 22:10

ramzes2