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. Sharing huge object between threads

Sharing huge object between threads

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 5 Posters 1.9k Views
  • 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.
  • G Offline
    G Offline
    gav007
    wrote on last edited by
    #1

    Hi,

    I developed a GUi application managing a huge object (a control unit description). This is made of hundreds of sub-objects, themselves made of sub-objects, ... Finally the object is really really huge.

    Every objects and sub-objects use Qt classes (mainly container classes).

    A communication thread is added to the application, bringing the need to share the huge object. If the standard Qt mechanism of signals and slots is used, hundreds of signals and slots should be created.

    Is there any other way to share Qt (meta) objects ?
    Is it viable to move the object (and all its sub-objects) to the communication thread and then to move it back to the main thread at the end of the communication thread ? I'm not really confident in such a solution.

    To be honest, if the answer is negative, I would rather replace all the Qt classes with equivalent in the C++ Standard Library. This would be too bad !

    Many thanks in advance for any support.

    Regards.

    J.HilkJ KroMignonK 2 Replies Last reply
    0
    • E Offline
      E Offline
      etla
      wrote on last edited by etla
      #2

      [I've removed this since it wasn't one bit helpful.]

      Code in haste; debug in leisure.

      G 1 Reply Last reply
      0
      • E etla

        [I've removed this since it wasn't one bit helpful.]

        G Offline
        G Offline
        gav007
        wrote on last edited by gav007
        #3

        @etla

        In Qt, (meta-)object lives in a thread. Attempting to access it from another thread will fail (crash). Here is a good introduction for the topic : https://doc.qt.io/qt-5/threads-qobject.html

        Regards

        E 1 Reply Last reply
        1
        • G gav007

          @etla

          In Qt, (meta-)object lives in a thread. Attempting to access it from another thread will fail (crash). Here is a good introduction for the topic : https://doc.qt.io/qt-5/threads-qobject.html

          Regards

          E Offline
          E Offline
          etla
          wrote on last edited by
          #4

          @gav007 I see. Now I've learned something new again.

          Apologies for the clutter then.

          Code in haste; debug in leisure.

          1 Reply Last reply
          0
          • G gav007

            Hi,

            I developed a GUi application managing a huge object (a control unit description). This is made of hundreds of sub-objects, themselves made of sub-objects, ... Finally the object is really really huge.

            Every objects and sub-objects use Qt classes (mainly container classes).

            A communication thread is added to the application, bringing the need to share the huge object. If the standard Qt mechanism of signals and slots is used, hundreds of signals and slots should be created.

            Is there any other way to share Qt (meta) objects ?
            Is it viable to move the object (and all its sub-objects) to the communication thread and then to move it back to the main thread at the end of the communication thread ? I'm not really confident in such a solution.

            To be honest, if the answer is negative, I would rather replace all the Qt classes with equivalent in the C++ Standard Library. This would be too bad !

            Many thanks in advance for any support.

            Regards.

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            Hi @gav007

            you can pass everything through Signal&Slots & across Threads, that has a copy constructor.

            Qt Container usually are not derived of Object and therefor have a copy constructor.

            You could, If that's not already the case - make an underlying struct, or class, that contains all your data. define a copy constructor for that class and register it as a metaobject. And you can simply pass such an struct as argument for your cross thread signal.


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            G 1 Reply Last reply
            3
            • G gav007

              Hi,

              I developed a GUi application managing a huge object (a control unit description). This is made of hundreds of sub-objects, themselves made of sub-objects, ... Finally the object is really really huge.

              Every objects and sub-objects use Qt classes (mainly container classes).

              A communication thread is added to the application, bringing the need to share the huge object. If the standard Qt mechanism of signals and slots is used, hundreds of signals and slots should be created.

              Is there any other way to share Qt (meta) objects ?
              Is it viable to move the object (and all its sub-objects) to the communication thread and then to move it back to the main thread at the end of the communication thread ? I'm not really confident in such a solution.

              To be honest, if the answer is negative, I would rather replace all the Qt classes with equivalent in the C++ Standard Library. This would be too bad !

              Many thanks in advance for any support.

              Regards.

              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by
              #6

              @gav007
              Removing QObject will not solve your problem. Why do you use QObject base classes if signals/slots are not relevant for you?

              The advantage of QObject, is to enable you to use signals/slots to share "information" between threads without having to implement yourself mutex/semaphore to protect the datas.

              This made it easy to move objects between threads, of course you need to implement copy constructor and register the classes.

              But that's not that difficult.

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              1 Reply Last reply
              0
              • J.HilkJ J.Hilk

                Hi @gav007

                you can pass everything through Signal&Slots & across Threads, that has a copy constructor.

                Qt Container usually are not derived of Object and therefor have a copy constructor.

                You could, If that's not already the case - make an underlying struct, or class, that contains all your data. define a copy constructor for that class and register it as a metaobject. And you can simply pass such an struct as argument for your cross thread signal.

                G Offline
                G Offline
                gav007
                wrote on last edited by
                #7

                Thanks for those answers.

                @J.Hilk

                Actually I did not wish copy the huge class. It hurts me a bit since the object is a singleton. And it needs to implement the copy constructor of decades of classes.

                However this could be the solution ... :-)

                In Qt containers, I include QString or QByteArray. You're right when you said they do not derive from QObject, but they are "meta-object". At least, they are not accessible from other threads than their own. This is more accurate when expressed like this ;-).

                @KroMignon

                I used Qt classes for convenience. It was easy since I'm developing with Qt Creator.

                This could be the lesson to learn from this : not using Qt classes in such a description object. If afterwards I need to transfer some info through signals and slots, it shall still be time to convert the standard types to Qt classes.

                The solution of the copy contsructor for registered class is not that difficult indeed. But, as I said above, it breaks the singleton design and makes me a bit upset ;-)

                Thanks again.
                Regards

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

                  Hi,

                  Why are you sharing such an object ? You wrote that it's a singleton so why not properly protect the access to whatever is inside it and then pass a pointer to it to the the threads that needs to use it ?

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

                  G 1 Reply Last reply
                  2
                  • SGaistS SGaist

                    Hi,

                    Why are you sharing such an object ? You wrote that it's a singleton so why not properly protect the access to whatever is inside it and then pass a pointer to it to the the threads that needs to use it ?

                    G Offline
                    G Offline
                    gav007
                    wrote on last edited by gav007
                    #9

                    @SGaist

                    That's what I did at first sight, but I experienced much crashes because of inaccessible content (for QString, QByteArray, ...).
                    For such a solution, I should change every getter to provide pointer ?

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

                      You should provide proper synchronisation using QMutex, QSemaphor etc. If you have read-write access to these properties.

                      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

                      • Login

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