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. -
Hi,
There's no move semantics involved especially between threads. Qt copies the data you pass as parameter(s) of your signal.
-
@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 safeQVector<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!