Kol*_*nya 6 c++ multithreading assignment-operator move-semantics c++11
尝试使简单的代码工作:
std::thread threadFoo;
std::thread&& threadBar = std::thread(threadFunction);
threadFoo = threadBar; // thread& operator=( thread&& other ); expected to be called
Run Code Online (Sandbox Code Playgroud)
得到错误:
使用已删除的函数'std :: thread&std :: thread :: operator =(const std :: thread&)'
我明确地定义threadBar为右值引用,而不是普通引用.为什么没有预期的操作员被调用?如何将一个线程移动到另一个线程?
谢谢!
R. *_*des 14
命名引用是左值.左值不绑定到右值参考.你需要使用std::move.
threadFoo = std::move(threadBar);
Run Code Online (Sandbox Code Playgroud)