gpt4 book ai didi

c++ - 等待 QMutex 断言

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:45:28 24 4
gpt4 key购买 nike

我发现即使是对 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

// ...
}

我得到断言,错误信息是:

ASSERT:'copy' in the thread\qmutex.cpp, line 525

最佳答案

您需要在调用 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

关于c++ - 等待 QMutex 断言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30986719/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com