[Qt-interest] Queued/DirectConnections for signals across threads
André Somers
andre at familiesomers.nl
Mon Dec 14 08:05:31 CET 2009
I expect* the following to happen:
In scenario 1, your loop in objectA will start to run. However, because your
loop will prevent the event loop from ever doing something again, it will
continue to run. It will never receive the stop signal. Unless you use a
processEvents() on your threads eventloop from inside your never-ending
loop, I guess.
In scenario 2, there is no way to tell. It partly depends on the
implementation of the run function. If that function first clears the stop
flag, that loop too may run forever. However, if it is simply a while
(!stop) {} loop, it might not run at all, because the stop signal may have
been delivered and executed before the run signal. However, there is no way
you can predict at what point contexts switch, or if they really run
simultaneously on different cores/CPUs. It could also run one or several
iterations of the loop, I think. So basically: what happens there, is
anyones guess.
Regards,
André
*) I am certainly no threading guru, so keep those grains of salt nearby
please.
Van: qt-interest-bounces at trolltech.com
[mailto:qt-interest-bounces at trolltech.com] Namens Preet
Verzonden: zondag 13 december 2009 20:07
Aan: qt-interest at trolltech.com
Onderwerp: [Qt-interest] Queued/DirectConnections for signals across threads
Hey all,
I have two threads, each with their own object. Both threads are running
exec() event loops so signals and slots will be handled by those two
threads. I have this one slot (slot Run) in one the objects (object A) that
has a while(someCondition) loop. This same object has another slot (slot
Stop) that will set 'someCondition' to false.
By default, any connections I make between the two objects will be of the
type QueuedConnection. My other object (object B) has signals to invoke the
Run and Stop slots. If I do something like:
ObjectB::someFunction()
{
someCondition = true;
emit signalRun; // queued connection
emit signalStop; // queued connection
std::cerr << "someFunction done" << std::endl;
}
I expect someFunction to emit two signals, then return immediately. I expect
object A's thread, if its not doing anything else, to be stuck in an
infinite while loop.
So if I do the following:
ObjectB::someFunction()
{
someCondition = true;
emit signalRun; // queued connection
emit signalStop; // direct connection
std::cerr << "someFunction done" << std::endl;
}
What is the resulting program flow?
Regards,
-Preet
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091214/3b93515f/attachment.html
More information about the Qt-interest-old
mailing list