标题:Qt线程同步问题
作者:songhuirong1
日期:2019-07-26 09:41
内容:
直接上代码:#include
#include
#include
#include
QMutex mutex;
QWaitCondition wc;
void testFunc()
{
qDebug()
#1 [fsu0413 07-26 22:30]
难道你的代码就没有一个用mutex保护的量来标志testFunc是否执行完吗?
#include
#include
#include
#include
QMutex mutex;
QWaitCondition wc;
bool flag=false;
void testFunc()
{
qDebug()
#2 回 fsu0413 的帖子 [songhuirong1 07-27 16:21]
fsu0413:难道你的代码就没有一个用mutex保护的量来标志testFunc是否执行完吗?
#include
#include
.......(2019-07-26 22:30)
非常感谢你的回复,你的代码应该也是可以解决问题的,只是我们实现的方式不一样而已,我的代码应该是没有问题的。我只是想向各位学习一下,看有没有更好的实现方式。你的这种方法给我提供了一种新的思路,我学习了。谢谢。如果我的代码有纰漏,还望指点一二。
#3 [songhuirong1 09-23 07:12]
新的例子代码,更加简洁。
template
bool common_kit::RemoteControlHelper::readRequest(int varid, int vartype, Type &data, int offset /*= 0*/)
{
if (m_dataStream.isNull())
{
Q_ASSERT(false);
return false;
}
if (gdp::core::INVALID_ID == m_mtManualId)
{
Q_ASSERT(false);
return false;
}
mn::common_title common_title_;
mn::common_title_item common_title_item_;
common_title_item_.varid = varid;
common_title_item_.offset = offset;
common_title_item_.vartype = vartype;
common_title_item_.length = sizeof(data);
common_title_.items.push_back(common_title_item_);
bool bOk = false;
QMutex mutex;
QMutexLocker mutexLocker(&mutex);
QWaitCondition wc;
int ret = m_dataStream->mt_post_common_read_request_by_id(m_mtManualId, common_title_, [&](ui ..
#4 [songhuirong1 09-23 07:14]
新的例子代码,更加简洁。
template
bool common_kit::RemoteControlHelper::readRequest(int varid, int vartype, Type &data, int offset /*= 0*/)
{
if (m_dataStream.isNull())
{
Q_ASSERT(false);
return false;
}
if (gdp::core::INVALID_ID == m_mtManualId)
{
Q_ASSERT(false);
return false;
}
mn::common_title common_title_;
mn::common_title_item common_title_item_;
common_title_item_.varid = varid;
common_title_item_.offset = offset;
common_title_item_.vartype = vartype;
common_title_item_.length = sizeof(data);
common_title_.items.push_back(common_title_item_);
bool bOk = false;
QMutex mutex;
QMutexLocker mutexLocker(&mutex);
QWaitCondition wc;
int ret = m_dataStream->mt_post_common_read_request_by_id(m_mtManualId, common_title_, [&](uint32_t mtManualId, ..