std::thread Why object is copied twice?

For anything you want to move or avoid copies, prefer move constructors and std::move.

But Why doesn’t this happen automatically for me?

Move in C++ is conservative. It generally will only move if you explicitly write std::move(). This was done because move semantics, if extended beyond very explicit circumstances, might break older code. Automatic-moves are often restricted to very careful set of circumstances for this reason.

In order to avoid copies in this situation, you need to shift a around by using std::move(a) (even when passing it into std::thread). The reason it makes a copy the first time around is because std::thread can’t guarantee that the value will exist after you have finished constructing the std::thread (and you haven’t explicitly moved it in). Thusly, it will do the safe thing and make a copy (not take a reference/pointer to what you passed in and store it: the code has no idea whether or not you’ll keep it alive or not).

Having both a move constructor and using std::move will allow the compiler to maximally and efficiently move your structure. If you’re using VC++ (with the CTP or not), you must explicitly write the move constructor, otherwise MSVC will (even sometimes erroneously) declare and use a Copy constructor.

Leave a Comment