Search code examples
c++multithreadingc++11move

std::move operation C++


Lines from Anthony Williams book:

The following example shows the use of std::move to transfer ownership of a dynamic object into a thread:

void process_big_object(std::unique_ptr<big_object>);

std::unique_ptr<big_object> p(new big_object);
p->prepare_data(42);
std::thread t(process_big_object,std::move(p));

By specifying std::move(p) in the std::thread constructor, the ownership of the big_object is transferred first into internal storage for the newly created thread and then into process_big_object.

I understand stack and heap; any idea, what actually is this internal storage ?

Why can't they transfer the ownership directly to process_big_object?


Solution

  • It means that the object will temporarily belong to the std::thread object until the thread actually starts.

    Internal storage here refers to the memory associated to the std::thread object. It could be a member variable, or just held in the stack during the constructor. Since this is implementation dependant, the general, and non-commital, "internal storage" term is used.