5

我有一个简单的 c++ std::vector 在里面,我正在存储线程,如下所示。您能否解释一下为什么带有注释“不编译”的行在编译期间显示错误?以及为什么带有注释“编译”的行有效?

#include<thread>
#include<vector>

using namespace std;
void abc() {}
int main()
{
   vector<thread> workers;
   workers.push_back(thread(abc)); // compiles
   thread t(abc);
   workers.push_back(t); // does not compile

   return 0;
 }

更新:我在 linux 上使用 g++ 4.4.6。下面是错误

[jim@cola c++]$ g++ -std=c++0x -pthread -g -Wall t.cpp -o t
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/x86_64-redhat-linux/bits/c++allocator.h:34,
                 from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/allocator.h:48,
                 from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/string:43,
                 from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/locale_classes.h:42,
                 from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/ios_base.h:43,
                 from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ios:43,
                 from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:40,
                 from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/iostream:40,
                 from t.cpp:1:
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/thread: In member function ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, const _Tp&) [with _Tp = std::thread]’:
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_vector.h:737:   instantiated from ‘void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = std::thread, _Alloc = std::allocator<std::thread>]’
t.cpp:29:   instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/thread:122: error: deleted function ‘std::thread::thread(const std::thread&)’
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ext/new_allocator.h:105: error: used here
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/vector:69,
                 from t.cpp:4:
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/thread: In member function ‘void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, _Args&& ...) [with _Args = const std::thread&, _Tp = std::thread, _Alloc = std::allocator<std::thread>]’:
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_vector.h:741:   instantiated from ‘void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = std::thread, _Alloc = std::allocator<std::thread>]’
t.cpp:29:   instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/thread:122: error: deleted function ‘std::thread::thread(const std::thread&)’
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/vector.tcc:314: error: used here
4

4 回答 4

13

您收到错误是因为std::thread不可复制并且您正在尝试将副本t插入向量中。

完成这项工作的唯一方法是:

workers.push_back(std::move(t));

然而,这意味着在你这样做之后,t不再代表一个线程(它所代表的线程被移动到向量中)。

于 2013-01-22T08:27:15.467 回答
2

原因是它std::thread有一个移动构造函数,但没有一个复制构造函数。

于 2013-01-22T08:28:20.563 回答
2

这是一种既不需要复制也不需要移动的更清洁、更快速的解决方案:

workers.emplace_back(abc);
于 2013-01-22T10:20:22.310 回答
1

因为std::threadis not copyable,您可以将其移至 vector :

   thread t(abc);
   workers.push_back(std::move(t));  

更好的解决方案是将智能指针存储在向量中:

std::vector<std::shared_ptr<std::thread>> workers;

因为在使用 lambda 时,无法捕获仅移动类型,因此解决方法是将仅移动类型存储在std::shared_ptr<std::thread>.

于 2013-01-22T08:28:22.083 回答