Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Move semantic between objects in different threads



  • 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.



  • 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


  • Lifetime Qt Champion

    Hi,

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



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



  • @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.



  • @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!



  • 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



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


Log in to reply