Anthony Williams 书中的台词:
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 thestd::thread
constructor, the ownership of thebig_object
is transferred first into internal storage for the newly created thread and then intoprocess_big_object
.
我了解堆栈和堆;知道吗,这个内部存储到底是什么?
为什么他们不能将所有权直接转移到 process_big_object
?
最佳答案
表示该对象会暂时属于std::thread
对象,直到线程真正启动。
此处的内部存储是指与std::thread
对象关联的内存。它可以是一个成员变量,或者只是在构造函数期间保存在堆栈中。由于这是依赖于实现的,因此使用了通用的、非 promise 的“内部存储”术语。
关于c++ - std::move 操作 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45825254/