c++ 我不能将互斥体与vector [duplicate]一起使用

qlzsbp2j  于 2023-06-25  发布在  其他
关注(0)|答案(2)|浏览(129)

此问题已在此处有答案

std::mutex as class member, and store class obect to container(3个答案)
How can I use something like std::vectorstd::mutex?(7个回答)
How should I deal with mutexes in movable types in C++?(5个答案)
4天前关闭。
我在编译以下代码时遇到问题。

  1. #include <vector>
  2. #include <mutex>
  3. class A
  4. {
  5. public:
  6. A(){}
  7. private:
  8. std::mutex a_mutex;
  9. };
  10. int main()
  11. {
  12. std::vector<A> v;
  13. A a;
  14. v.push_back(a);
  15. return 0;
  16. }

这就是错误。

  1. $ g++ test__use_of_deleted_function.cpp
  2. In file included from /usr/include/c++/8/x86_64-redhat-linux/bits/c++allocator.h:33,
  3. from /usr/include/c++/8/bits/allocator.h:46,
  4. from /usr/include/c++/8/string:41,
  5. from test__use_of_delete_function.cpp:1:
  6. /usr/include/c++/8/ext/new_allocator.h: In instantiation of void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = A; _Args = {const A&}; _Tp = A]’:
  7. /usr/include/c++/8/bits/alloc_traits.h:475:4: required from static void std::allocator_traits<std::allocator<_CharT> >::construct(std::allocator_traits<std::allocator<_CharT> >::allocator_type&, _Up*, _Args&& ...) [with _Up = A; _Args = {const A&}; _Tp = A; std::allocator_traits<std::allocator<_CharT> >::allocator_type = std::allocator<A>]’
  8. /usr/include/c++/8/bits/stl_vector.h:1079:30: required from void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = A; _Alloc = std::allocator<A>; std::vector<_Tp, _Alloc>::value_type = A]’
  9. test__use_of_delete_function.cpp:19:17: required from here
  10. /usr/include/c++/8/ext/new_allocator.h:136:4: error: use of deleted function A::A(const A&)’
  11. { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
  12. ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  13. test__use_of_delete_function.cpp:6:7: note: A::A(const A&)’ is implicitly deleted because the default definition would be ill-formed:
  14. class A
  15. ^
  16. test__use_of_delete_function.cpp:6:7: error: use of deleted function std::mutex::mutex(const std::mutex&)’
  17. In file included from /usr/include/c++/8/mutex:43,
  18. from test__use_of_delete_function.cpp:3:
  19. /usr/include/c++/8/bits/std_mutex.h:97:5: note: declared here
  20. mutex(const mutex&) = delete;
  21. ^~~~~
  22. In file included from /usr/include/c++/8/vector:62,
  23. from test__use_of_delete_function.cpp:2:
  24. /usr/include/c++/8/bits/stl_construct.h: In instantiation of void std::_Construct(_T1*, _Args&& ...) [with _T1 = A; _Args = {A}]’:
  25. /usr/include/c++/8/bits/stl_uninitialized.h:83:18: required from static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::move_iterator<A*>; _ForwardIterator = A*; bool _TrivialValueTypes = false]’
  26. /usr/include/c++/8/bits/stl_uninitialized.h:134:15: required from _ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::move_iterator<A*>; _ForwardIterator = A*]’
  27. /usr/include/c++/8/bits/stl_uninitialized.h:289:37: required from _ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = std::move_iterator<A*>; _ForwardIterator = A*; _Tp = A]’
  28. /usr/include/c++/8/bits/stl_uninitialized.h:311:2: required from _ForwardIterator std::__uninitialized_move_if_noexcept_a(_InputIterator, _InputIterator, _ForwardIterator, _Allocator&) [with _InputIterator = A*; _ForwardIterator = A*; _Allocator = std::allocator<A>]’
  29. /usr/include/c++/8/bits/vector.tcc:447:6: required from void std::vector<_Tp, _Alloc>::_M_realloc_insert(std::vector<_Tp, _Alloc>::iterator, _Args&& ...) [with _Args = {const A&}; _Tp = A; _Alloc = std::allocator<A>; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<A*, std::vector<A> >; typename std::_Vector_base<_Tp, _Alloc>::pointer = A*]’
  30. /usr/include/c++/8/bits/stl_vector.h:1085:4: required from void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = A; _Alloc = std::allocator<A>; std::vector<_Tp, _Alloc>::value_type = A]’
  31. test__use_of_delete_function.cpp:19:17: required from here
  32. /usr/include/c++/8/bits/stl_construct.h:75:7: error: use of deleted function A::A(A&&)’
  33. { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
  34. ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  35. test__use_of_delete_function.cpp:6:7: note: A::A(A&&)’ is implicitly deleted because the default definition would be ill-formed:
  36. class A
  37. ^
  38. test__use_of_delete_function.cpp:6:7: error: use of deleted function std::mutex::mutex(const std::mutex&)’
  39. In file included from /usr/include/c++/8/mutex:43,
  40. from test__use_of_delete_function.cpp:3:
  41. /usr/include/c++/8/bits/std_mutex.h:97:5: note: declared here
  42. mutex(const mutex&) = delete;
  43. ^~~~~
k2fxgqgv

k2fxgqgv1#

因为std::mutex的复制构造函数被删除了,所以需要为A定义一个显式的复制构造函数。
A(const A& other) { ... }
您可能想看看C++的惯用规则4What is the Rule of Four (and a half)?

kqhtkvqz

kqhtkvqz2#

std::mutex既不能复制也不能移动。
herehere等答案所示,std::mutex成员应该用于实现该类的线程安全复制和移动操作,同时复制和移动该类的其他成员。
否则,如here所示,可以持有指向向量中类的智能指针(std::vector<std::unique_ptr<A>> v;v.push_back(std::make_unique<A>());)。然而,在实际情况下,单独这样做可能不够有用。

相关问题