我是新来的Qt和QSharedPointer在信号中传递的一些问题。我正在使用两个线程(UI和一个工作人员)。工作人员使用包含自定义QSharedPointer的QObject的信号向UI发送信号:
class MyObject : QObject {...}
class Window : public QWidget {
Q_OBJECT
public slots:
void onFound(QSharedPointer<MyObject>);
}
class Worker : public QObject {
Q_OBJECT
public signals:
void found(QSharedPointer<MyObject>);
}我将工人found与windows onFound与Qt::QueuedConnection连接,因为他们生活在不同的线程中,因此通信必须是异步的。
现在,当我传递引用我的对象的最后一个QSharedPointer时,我观察到了它的行为:
void*的指针,并对其进行排序。这不是我所期望的--尽管这是合理的。一般情况下,QSharedPointer被设计成通过这种方式传递信号吗?如果是这样的话,是否有一种机制可以在引用排队时保留它呢?
我考虑了折叠式的解决方案,但我对这两种方法都不太满意:
Qt::DirectConnection,但我仍然需要以某种方式切换线程(与以前一样)std::function参数的新信号/时隙,用于传递要在目标线程中执行的lambda函数,并捕获共享指针的副本。(这是我目前的解决方案,但不是很优雅,不是吗?)你还有什么其他的建议或想法吗?
发布于 2018-03-06 16:10:51
信号返回不会破坏相应的对象。QMetaObject::activate调用复制共享指针。下面是send信号的实现:
// SIGNAL 0
void IO::send(const QSharedPointer<Unique> & _t1)
{
void *_a[] = { nullptr, const_cast<void*>(reinterpret_cast<const void*>(&_t1)) };
QMetaObject::activate(this, &staticMetaObject, 0, _a);
}您可能正在经历一场竞赛:当发出信号的线程恢复执行时,目标线程已经接收到对象。因此,在发射线程中,该对象就消失了--因为,到了那个时候,它已经消失了。然而,目标对象可以很好地接收实例。效果很好。
下面的示例说明了它在单线程和多线程情况下都能工作,然后通过确保目标线程总是赢得比赛来再现您的问题:
// https://github.com/KubaO/stackoverflown/tree/master/questions/shared-pointer-queued-49133331
#include <QtCore>
class Unique : public QObject {
Q_OBJECT
int const m_id = []{
static QAtomicInteger<int> ctr;
return ctr.fetchAndAddOrdered(1);
}();
public:
int id() const { return m_id; }
};
class IO : public QObject {
Q_OBJECT
int m_lastId = -1;
public:
Q_SIGNAL void send(const QSharedPointer<Unique> &);
Q_SLOT void receive(const QSharedPointer<Unique> & u) {
m_lastId = u->id();
}
int lastId() const { return m_lastId; }
};
int main(int argc, char ** argv) {
Q_ASSERT(QT_VERSION >= QT_VERSION_CHECK(5,9,0));
QCoreApplication app{argc, argv};
IO src, dst;
QObject::connect(&src, &IO::send, &dst, &IO::receive, Qt::QueuedConnection);
QSharedPointer<Unique> u;
QWeakPointer<Unique> alive;
int id = -1;
// Single-threaded case
alive = (u.reset(new Unique), u);
id = u->id();
Q_ASSERT(dst.lastId() != id); // the destination hasn't seen the object yet
emit src.send(u);
u.reset();
Q_ASSERT(!u); // we gave up ownership of the object
Q_ASSERT(dst.lastId() != id); // the destination mustn't seen the object yet
Q_ASSERT(alive); // the object must be still alive
app.processEvents();
Q_ASSERT(dst.lastId() == id); // the destination must have seen the object now
Q_ASSERT(!alive); // the object should have been destroyed by now
// Multi-threaded setup
struct Thread : QThread { ~Thread() { quit(); wait(); } } worker;
worker.start();
dst.moveToThread(&worker);
QSemaphore s_src, s_dst;
// This thread wins the race
alive = (u.reset(new Unique), u);
id = u->id();
Q_ASSERT(dst.lastId() != id);
QTimer::singleShot(0, &dst, [&]{ s_src.release(); s_dst.acquire(); });
// stop the thread
s_src.acquire(); // wait for thread to be stopped
emit src.send(u);
QTimer::singleShot(0, &dst, [&]{ s_src.release(); });
// resume the main thread when done
u.reset();
Q_ASSERT(!u);
Q_ASSERT(alive); // we won the race: the object must be still alive
s_dst.release(); // get the thread running
s_src.acquire(); // wait for the thread to be done
Q_ASSERT(dst.lastId() == id);
Q_ASSERT(!alive);
// The other thread wins the race
alive = (u.reset(new Unique), u);
id = u->id();
Q_ASSERT(dst.lastId() != id);
emit src.send(u);
QTimer::singleShot(0, &dst, [&]{ s_src.release(); });
// resume the main thread when done
u.reset();
s_src.acquire(); // wait for worker thread to be done
Q_ASSERT(!u);
Q_ASSERT(!alive); // we lost the race: the object must be gone
Q_ASSERT(dst.lastId() == id); // yet the destination has received it!
// Ensure the rendezvous logic didn't mess up
Q_ASSERT(id == 2);
Q_ASSERT(!s_src.available());
Q_ASSERT(!s_dst.available());
}
#include "main.moc"https://stackoverflow.com/questions/49133331
复制相似问题