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

Move semantic between objects in different threads

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 628 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 1 Jun 2020, 12:18 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
    • V Offline
      V Offline
      VRonin
      wrote on 2 Jun 2020, 12:53 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 Jun 2020, 13:00
      2
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 1 Jun 2020, 17:07 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 2 Jun 2020, 10:31
        4
        • S SGaist
          1 Jun 2020, 17:07

          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 2 Jun 2020, 10:31 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?

          V 1 Reply Last reply 2 Jun 2020, 10:55
          0
          • S St.Stanislav
            2 Jun 2020, 10:31

            @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?

            V Offline
            V Offline
            VRonin
            wrote on 2 Jun 2020, 10:55 last edited by VRonin 6 Feb 2020, 10:56
            #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 2 Jun 2020, 11:58
            3
            • V VRonin
              2 Jun 2020, 10:55

              @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 2 Jun 2020, 11:58 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
              • V Offline
                V Offline
                VRonin
                wrote on 2 Jun 2020, 12:53 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 Jun 2020, 13:00
                2
                • V VRonin
                  2 Jun 2020, 12:53

                  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 2 Jun 2020, 13:00 last edited by
                  #7

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

                  1 Reply Last reply
                  0

                  4/7

                  2 Jun 2020, 10:55

                  • Login

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