Multi-threading behavior of signals and slots
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.
Re: Multi-threading behavior of signals and slots
Quote:
Originally Posted by
T_h_e_o
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.
Quote:
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?
Code:
#include <QCoreApplication>
#include <QThread>
#include <QtDebug>
Q_OBJECT
public:
A(){}
public slots:
void emitSignal() { emit sig1(); }
signals:
void sig1();
};
Q_OBJECT
public:
C(){}
public slots:
void someSlot() { qDebug() << Q_FUNC_INFO; }
};
#include "main.moc"
int main(int argc, char **argv) {
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();
}
Re: Multi-threading behavior of signals and slots
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.
Re: Multi-threading behavior of signals and slots
Quote:
Originally Posted by
T_h_e_o
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.
Quote:
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.