c++ - std::move 操作 C++

标签 c++ multithreading c++11 move

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 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.

我了解堆栈和堆;知道吗,这个内部存储到底是什么?

为什么他们不能将所有权直接转移到 process_big_object

最佳答案

表示该对象会暂时属于std::thread对象,直到线程真正启动。

此处的内部存储是指与std::thread 对象关联的内存。它可以是一个成员变量,或者只是在构造函数期间保存在堆栈中。由于这是依赖于实现的,因此使用了通用的、非 promise 的“内部存储”术语。

关于c++ - std::move 操作 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45825254/

相关文章:

c++ - std::get_time 在 g++ 和 clang++ 中被破坏了吗?

c++ - 有没有办法让 MFC 应用程序同时在控制台和窗口中工作

带删除的 C++ 映射迭代

c++ - 如何让我的编译器更笨(错误的索引)?

c++ - 将 std::map 与 std::thread 一起使用的意外行为

c++ - 使用相同的窗口类多次调用 RegisterWindow 的副作用?

c++ - sql 选择顺序的强制转换规范的字符值无效

java - 如何创建另一个 "version"的 process()

c# - 如何按顺序运行 C# 线程

java - 为什么我的服务的 AsyncTask 会阻止来自主 Activity 的 AsyncTasks?