Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 移动std::线程_C++_Multithreading_C++11_Move Semantics_Assignment Operator - Fatal编程技术网

C++ 移动std::线程

C++ 移动std::线程,c++,multithreading,c++11,move-semantics,assignment-operator,C++,Multithreading,C++11,Move Semantics,Assignment Operator,尝试让简单的代码工作: std::thread threadFoo; std::thread&& threadBar = std::thread(threadFunction); threadFoo = threadBar; // thread& operator=( thread&& other ); expected to be called 获取错误: 已删除函数“std::thread&std::thread::operator=(const

尝试让简单的代码工作:

std::thread threadFoo;

std::thread&& threadBar = std::thread(threadFunction);

threadFoo = threadBar; // thread& operator=( thread&& other ); expected to be called
获取错误:

已删除函数“std::thread&std::thread::operator=(const)”的使用 std::thread&)'

我明确地将
threadBar
定义为右值引用,而不是普通引用。为什么不调用预期的运算符?如何将一个线程移动到另一个线程


谢谢大家!

命名的引用是左值。左值不绑定到右值引用。您需要使用
std::move

threadFoo = std::move(threadBar);
另见。这可以按如下方式实施:

std::thread threadFoo;
std::thread threadBar = std::thread(threadFunction);
threadBar.swap(threadFoo);

谢谢你的回答。使用预期运算符的唯一方法是使用像
threadFoo=std::thread(threadFunction)这样的临时线程对象,这对我来说正确吗?从
std::move
获得的临时或未命名引用。感谢您的解释!