Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QObject communiction between objects in different threads
Forum Updated to NodeBB v4.3 + New Features

QObject communiction between objects in different threads

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 309 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RobertoSid
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by
      #2

      https://wiki.qt.io/QThreads_general_usage

      1 Reply Last reply
      0
      • R Offline
        R Offline
        RobertoSid
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • R Offline
            R Offline
            RobertoSid
            wrote on last edited by
            #5

            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.

            https://doc.qt.io/qt-5/qthread.html

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved