Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Move semantic between objects in different threads

    General and Desktop
    3
    7
    248
    Loading More Posts
    • 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
      St.Stanislav last edited by

      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 Reply Quote 0
      • VRonin
        VRonin last edited by

        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 Reply Quote 2
        • SGaist
          SGaist Lifetime Qt Champion last edited by

          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 Reply Quote 4
          • S
            St.Stanislav @SGaist last edited by

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

            VRonin 1 Reply Last reply Reply Quote 0
            • VRonin
              VRonin @St.Stanislav last edited by 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.

              "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 Reply Quote 3
              • S
                St.Stanislav @VRonin last edited by

                @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 Reply Quote 0
                • VRonin
                  VRonin last edited by

                  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 Reply Quote 2
                  • S
                    St.Stanislav @VRonin last edited by

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

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post