View Full Version : Multi-threading behavior of signals and slots
T_h_e_o
11th January 2013, 09:51
Suppose I have an object A living in thread a and object C living in thread c. Object A has a signal sig1 which is connected to a particular slot of object C (default connection, which means in this case queued connection since it is cross-thread).
Suppose thread a is running and object A emits signal sig1 twice before thread c runs. After that, thread c runs. Will the slot of object C be called once or twice?
Similarly to the situation above, add a thread b with object B which has a signal sig2 that is connected to the same slot of object C. Situation: thread a runs, object A emits signal sig1. Then thread b runs, object B emits signal sig2. Then thread c runs. Will the slot of object C be called once or twice?
Thank you for any insights.
wysota
11th January 2013, 09:58
Suppose thread a is running and object A emits signal sig1 twice before thread c runs. After that, thread c runs. Will the slot of object C be called once or twice?
Twice.
Similarly to the situation above, add a thread b with object B which has a signal sig2 that is connected to the same slot of object C. Situation: thread a runs, object A emits signal sig1. Then thread b runs, object B emits signal sig2. Then thread c runs. Will the slot of object C be called once or twice?
Twice.
I know I posted a reply to your thread pretty quickly, but wouldn't it give you more satisfaction to just write an example program and test it yourself?
#include <QCoreApplication>
#include <QThread>
#include <QtDebug>
class A : public QObject {
Q_OBJECT
public:
A(){}
public slots:
void emitSignal() { emit sig1(); }
signals:
void sig1();
};
class C : public QObject {
Q_OBJECT
public:
C(){}
public slots:
void someSlot() { qDebug() << Q_FUNC_INFO; }
};
#include "main.moc"
int main(int argc, char **argv) {
QCoreApplication app(argc, argv);
QThread t;
A *a = new A;
C *c = new C;
QObject::connect(a, SIGNAL(sig1()), c, SLOT(someSlot()));
c->moveToThread(&t);
a->emitSignal();
a->emitSignal();
t.start();
return app.exec();
}
T_h_e_o
11th January 2013, 10:05
Wysota, thanks, that was really fast!
Considering your reputation I trust your answer is 100% correct, but viewing your example: how do you guarantee that the two signals of a are *both* emitted before the main thread is executed?
As for your question: I'm still in the process of thinking about different solutions, so no software available yet to easily put some test code into. Wouldn't have expected that a piece of code to test it could be *that* simple... Guess that's a matter of experience (hey, I didn't post this in the newbie forum for nothing...).
Anyway, thanks again.
wysota
11th January 2013, 11:04
how do you guarantee that the two signals of a are *both* emitted before the main thread is executed?
Signals are always emitted immediately. It is slots that can be executed later depending on thread affinity.
As for your question: I'm still in the process of thinking about different solutions, so no software available yet to easily put some test code into.
You can see a complete test case in my previous post, there is no need for any special software apart Qt itself.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.