QObject communiction between objects in different threads
-
Hello guys!
I hope that you are safe and well.
TL;DR
I have two object that interchange messages using signal/slot mechanism. One of this objects can take a long time processing something and then, as an experiment, I moved it to a new thread.
The program worked better even not doing any treatment between the new thread and parent object signals/slots. Is it acceptable?
I am studying and developing an application based on System Bus Architecture. So, there is a BUS where modules are connected and all communication happens by messages interchange. Modules and BUS are interconnected by signal/slot mechanism.
Everything is working as expected but I started to deal with a new module, that I called C. This module handle synchronous requisitions that can take an impredictable amount of time to be processed.
Because of the primitive nature of this development, all modules are running in main thread and this leads a slow responses when module C is processing something. All modules are loaded in the start of program and remains until the end of execution.
First, a disclaimer: it was just an experiment!
So, just for test I did the interconnection between module C and BUS.
[ C module ] <--------> [ BUS ]
Object BUS has signal/slots configured with C module in this step
And I moved "C module" object to a new thread using QThread.
QThread l_thread{}; l_cModule->moveToThread(&l_thread); l_thread.start();
After that, the code was compiled with no issues and the behaviour is what I was expecting but with more effort. The comunication between Bus and C module happens flawless and there is no slow behaviour of the system.
However I feel no confidence that it should be acceptable. Ok, I can treat the thread signals and inject a way to communicate with the object inside the thread. But why still mechanism signal/slot working pretty well even without I explicitly signal/slot in thread and parent object?
Is acceptable using thread in this way?
Could someone help to understand what would be a good practice? -
-
I think I found a good start here: https://www.qt.io/blog/2010/06/17/youre-doing-it-wrong
if I understand correctly, QThread could be used in a way that I've described. For this reason, if I have an object running, I can move it to a thread and the framework will guarantee that the connexions will remain available.
And If I call a method using signal/slot that was created before moveToThread, this connection will remain working.
Am I right?
-
Hi and welcome to devnet,
That eleven years old blog post had its use at that time however, QThread's implementation has improved and the blog is not accurate anymore. Just read QThread's documentation. It contains and explains both subclass and worker object.
-
Thank you, @SGaist !
You're completely right.
The code inside the Worker's slot would then execute in a separate thread. However, you are free to connect the Worker's slots to any signal, from any object, in any thread. It is > safe to connect signals and slots across different threads, thanks to a mechanism called queued connections.