c++ 从成员函数创建新线程并移动对象和整个对象
c++ Creating a new thread from a member function and moving the object and the entire
根据下面的代码,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();
........
}
就目前而言,您的main
将调用 std::terminate
,因为您丢弃了可连接的std::thread
。
如果您加入它,main
将阻止,直到线程完成。 object
将在整个Run
期间保持活力。
如果将其分离,main
可能会在线程之前结束,object
将不复存在,并且myClass1::Run
中的this
将无效。未定义的行为。
您的代码
class myClass1 {
myClass2 obj;
public:
// Take by rvalue, uses the move constructor for obj
myClass1(myClass2 && obj) : obj(obj) {}
std::thread spawn() {
return std::thread([this]
{
// This is suspicious, but safe
auto self = std::move(*this);
self.Run();
});
}
void Run() {
std::cout << "new thread" << std::endl;
}
}
int main(){
// new is not required
myClass1 object(myClass2("test"));
object.spawn().join();
/* other stuff, not involving object */
return 0;
}
更像是整理
class myClass1 {
myClass2 obj;
public:
// Take by rvalue, uses the move constructor for obj
myClass1(myClass2 && obj) : obj(obj) {}
void Run() {
std::cout << "new thread" << std::endl;
}
}
int main() {
// Just create the instance of myClass1 as a parameter to `std::thread`'s constructor
std::thread(&myClass1::Run, myClass1(myClass2("test"))).join();
/* other stuff */
return 0;
}
不;创建线程不会神奇地使线程获得该内存的所有权。 如果在堆栈上创建对象,请创建一个使用该对象的线程;然后展开堆栈,破坏物体;当线程仍在运行时,您将有未定义的行为。
如果要将某些数据的所有权授予线程,最简单的方法是使用共享指针。
相关文章:
- 将对象移动到std::shared_ptr
- std::使用内部对象移动 - 与调用不匹配
- 基于范围的 for 循环将对象移动到另一个容器中?
- 将列表的对象C++移动到另一个对象
- 从移出对象移动
- C++如何将对象移动到 nullptr
- Qt 通过将对象移动到线程来跨线程发出信号
- 如何将对象移动到std::async()
- 2D 阵列/网格中的对象移动
- 无法将套接字对象移动到std::vector中
- 在将对象移动到另一个线程后,不能简单地删除该对象
- 如何将相互依赖的对象移动到一起并维护内部引用
- 在OpenGL中将2D对象移动到一个点
- 如何将对象移动到未初始化的内存中
- 复制构造函数并通过跟踪对象移动语义
- SFML -将对象移动到coord
- 在堆栈程序中实现一个计数器,用于计算对象移动的次数
- Std::将Std::unique_ptr对象移动到STL容器中.(MSVC编译器问题)
- 将临时对象移动到矢量中
- 将c++对象移动到内存位置