【发布时间】:2018-04-19 08:19:38
【问题描述】:
根据下面的代码,myClass1 对象和 myClass2 obj(这是 myClass1 对象的成员)是否会随着它们的内存移动到新线程(如 std::move())?
class myClass1{
public:
myClass2 obj;
myClass1(myClass2 * obj) {
this.obj = *obj;
}
thread spawn() {
return std::thread([this] { this->Run(); });
}
void Run() {
cout << "new thread" << endl;
}
}
myClass2{
public :
string str;
MyClass2(string str){
this.str = str;
}
}
int main(){
myClass1 object(new myClass2("test"));
thread t = object.spawn();
t.join();
........
}
【问题讨论】:
-
这还能编译吗?
-
请尝试创建Minimal, Complete, and Verifiable Example 并展示给我们。
-
它只是为了展示概念,但如果你想编译它,它应该有一个新线程的连接和一个 myclass2 的定义:)
-
“它应该有...”你应该提供它
-
您仍然没有可编译的示例。在您的构造函数中,
this.obj = obj;是一个类型错误。您想要myClass2类型的值 还是指向myClass2的(n 拥有)原始指针?
标签: c++ multithreading c++11 memory