[Interest] Emitting signal from QThread::run()

Prav pr12og2 at programist.ru
Sun Feb 28 15:33:34 CET 2016


> In the context of the previous example, I wasn't entirely sure if this was a
> possibility:
> 1. The QThread-derived instance is created in the thread of MainWindow.
> 2. That instance is not moved to other thread, so it keeps the thread affinity
> of the MainWindow.
> 3. When a signal is emitted in run(), the thread affinity checked is somehow
> "stored" in the instance, and since moveToThread did not change it, it's still
> the same of MainWindow.
> Now I have the confirmation that the 3rd point is wrong, which is good. :)

Yes ... hard to expect from names that inside of QThread::run() method the current thread is not QThread::thread() !

I think the hardness of understanding "QThread-ing" comes from bad term like ... object "live in" thread.
Which makes feel that object is like "placed inside" some bounds of QThread.

Term "affinity" is not better as soon as (https://en.wikipedia.org/wiki/Processor_affinity):
Affinity make so that "... process or thread will execute ONLY on the designated CPU ..."

Like in this example makes people consider that QThread::run is executed by thread where QThread is "living in".
Or very stragly to find out at some point that newly created QThread object (which is QObject)
does not "lives in" newly created thread ... which can blow mind ...
and many people start use QThreads in inapropriate way ...
and this QThread-ing became one of hardest to get themes in Qt.

But actually QObject is "living in all threads" (it is in the process memory) ... and ANY thread can call its methods.
Better to think (and say) that object ASKed
to execute all object's QUEUED "connections" (which are object's slots and events methods)
by some thread (which is by default QThread::currentThread() at the moment of object creation)

And I think it would saved many hours of understanding for many people if long ago someone
called "moveToThread" method as "moveConnectionsToThread".
Only reuest to execute queued Connections is "moved"!
NOT the object itself (memory, data)! And even not the execution of all other methods.
So it is very strange that this weak relation was called as "living in" or "affinity".


In this thinking:
In this example we see that QThread::run() method is not a slot (not a "connection") of QThread ...
SO we should not expect that it is executed by thread where QThread-object
registered its "connections" processing (which is original thread). That is why it is not QThread::thread().


Moreover ... even "connections" can be executed by ANY thread of the process
just as regular methods of C++ language or
by QObject::connect with Qt::DirectConnection flag.

So in any QObject's code the current thread could be not a
      QObject::thread()
but is
      QThread::currentThread()


PS
Alejandro, I get that you know all this but I was thinking that this remark could probably be helpfull
for many others who scratch their heads because of "living in" questions like this.




More information about the Interest mailing list