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. Move semantic between objects in different threads
QtWS25 Last Chance

Move semantic between objects in different threads

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 615 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.
  • S Offline
    S Offline
    St.Stanislav
    wrote on last edited by
    #1

    Hello there!
    I have the following situation: I have to get information from serial port parse it into QVector<float> and pass in the other object for the further processing. This processing object is on other thread, so I was going to use signal-slot mechanism. I could not find a definetely correct answer for the following question: what is a correct way to use move semantics to pass data between objects in differenth threads with usage of a signal-slot mechanism. I would be appreciated for any information and thank you in advance.

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #6

      Passing by pointer/reference is infinitesimally more efficient in the same thread but if you pass them across threads using signal/slot it's going to be the same as passing by value

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

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

        Hi,

        There's no move semantics involved especially between threads. Qt copies the data you pass as parameter(s) of your signal.

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

        S 1 Reply Last reply
        4
        • SGaistS SGaist

          Hi,

          There's no move semantics involved especially between threads. Qt copies the data you pass as parameter(s) of your signal.

          S Offline
          S Offline
          St.Stanislav
          wrote on last edited by
          #3

          @SGaist Thank you for the answer, that's why I found nothing special about this :) So the closest way that I could use (and have already tried successfully) to "emulate" move semantic with QSharedPointer, right?

          VRoninV 1 Reply Last reply
          0
          • S St.Stanislav

            @SGaist Thank you for the answer, that's why I found nothing special about this :) So the closest way that I could use (and have already tried successfully) to "emulate" move semantic with QSharedPointer, right?

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #4

            @St-Stanislav said in Move semantic between objects in different threads:

            "emulate" move semantic with QSharedPointer, right?

            No, QSharedPointer is just a smart pointer, not thread safe

            QVector<float> and pass in the other object

            So here is the thing, you don't need to implement move semantic for Qt types. Basically all the Qt types did move semantic before it was cool (i.e. before it was included in the standard) so the copy constructor is already as inexpensive as it gets (a pointer assignment and an integer increment). This is normally referred to as implicit sharing. The moral of the story here is that you shouldn't worry about move semantic at all for Qt types (either in the same thread or a different thread). Just copy them around and let Qt do the hard work.

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            S 1 Reply Last reply
            3
            • VRoninV VRonin

              @St-Stanislav said in Move semantic between objects in different threads:

              "emulate" move semantic with QSharedPointer, right?

              No, QSharedPointer is just a smart pointer, not thread safe

              QVector<float> and pass in the other object

              So here is the thing, you don't need to implement move semantic for Qt types. Basically all the Qt types did move semantic before it was cool (i.e. before it was included in the standard) so the copy constructor is already as inexpensive as it gets (a pointer assignment and an integer increment). This is normally referred to as implicit sharing. The moral of the story here is that you shouldn't worry about move semantic at all for Qt types (either in the same thread or a different thread). Just copy them around and let Qt do the hard work.

              S Offline
              S Offline
              St.Stanislav
              wrote on last edited by
              #5

              @VRonin Thank you for the detailed answer, I have last point that want to decide and understand: should I use passing by reference or coping process will be with the same efficiency? I have found old topic with the same discussion and answers from you, but what it correct way now, in 5.15.0? Is it the same?

              Thank you in advance for helping me!

              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #6

                Passing by pointer/reference is infinitesimally more efficient in the same thread but if you pass them across threads using signal/slot it's going to be the same as passing by value

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                S 1 Reply Last reply
                2
                • VRoninV VRonin

                  Passing by pointer/reference is infinitesimally more efficient in the same thread but if you pass them across threads using signal/slot it's going to be the same as passing by value

                  S Offline
                  S Offline
                  St.Stanislav
                  wrote on last edited by
                  #7

                  @VRonin Yes, I have mentioned passing between threads, thank you!

                  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