Christian Grabowski Christian Grabowski - 4 months ago 34
C++ Question

Is there a performance cost using std::move vs using a pointer to the thread?

I imagine std::move() has a bit more of a performance cost in the context of something like:

std::thread thrd(&func, this);
someArrOfThreads[0] = std::move(thrd);


std::thread *thrd = new std::thread(&func, this);
someArrOfThreadPointers[0] = thrd;

Is this true? And if so, is it a matter of
changes the boundaries of memory of the thread or something else?

I realize there is the difference that in the first I am actually assigning a value of the array to the thread, and the other being the pointer to the thread, the thread stays in its address(es) in the second.


The only thing that std::move does is turn a lvalue into an rvalue reference. This then causes the rvalue reference version of tge assigment operator to be call. From a code generation point of view it is a noop.

The rvalue overload is likely to be more efficient since it is allowed to move class internal as opposed to performing a deep copy.