QByteArray 通过 QueuedConnection 按值发出和连接并附加到竞争条件?

QByteArray emited and connected by value through QueuedConnection and appended to race condition?

我有一个信号 newData(int type, QByteArray data) 作为 queuedConnection 连接到 slotNewData(int type, QByteArray data),然后我发出两个数据回调,如此处所示

emit newData(OEI_DataParserV2_base::OEI_CBT_DOUBLE, dp_v2_databuff_);
uint32_t sec;
uint32_t usec;
dataParserV2->getDataTimestamp(sec,usec);
dp_v2_databuff_.append(reinterpret_cast<const char*>(&usec), sizeof(usec));
dp_v2_databuff_.append(reinterpret_cast<const char*>(&sec), sizeof(sec));
emit newData(OEI_DataParserV2_base::OEI_CBT_TIMESTAMP_DOUBLE, dp_v2_databuff_);

我希望我的插槽接收没有附加时间戳的数据,然后接收带有附加时间戳的数据。但是,我有时会看到返回的数据类型为 OEI_CBT_DOUBLE,并附加了额外的时间戳。我看到 QByteArray 是 implicitly shared 但数据应该在写入时复制。 Qt 文档特别说明

Note that atomic reference counting does not guarantee thread-safety. Proper locking should be used when sharing an instance of an implicitly shared class between threads. This is the same requirement placed on all reentrant classes, shared or not. Atomic reference counting does, however, guarantee that a thread working on its own, local instance of an implicitly shared class is safe. We recommend using signals and slots to pass data between threads, as this can be done without the need for any explicit locking.

QByteArray::append 不应该创建我的 dp_v2_databuff_ 的深层副本吗?我应该按 const ref 还是按值通过 queuedConnections 传递我的 QByteArrays?

因此,引发此讨论的错误现已解决。 QByteArray 被附加到此代码的每次迭代中,因此变得越来越大,过去的时间戳粘在它的末尾。