【发布时间】:2020-08-11 02:03:32
【问题描述】:
喜欢做一个线程安全的C++数据结构如下。
struct infos{
int crowdinfos[10][horGridNums*verGridNums];
int cameraSourceID;
static int idx;
std::mutex mutex;
};
mutex.lock 和 unlock 将用于线程安全。
编辑: 在我的头文件中,我将有一个信息向量。
std::vector<infos> c_infos;
当我使用 g++ 构建时,出现错误
/usr/include/c++/7/bits/stl_construct.h:75:7: error: use of deleted function ‘infos::infos(const infos&)’
{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from gstdsexample.cpp:29:0:
gstdsexample.h:66:8: note: ‘infos::infos(const infos&)’ is implicitly deleted because the default definition would be ill-formed:
struct infos{
^~~~~
gstdsexample.h:66:8: error: use of deleted function ‘std::mutex::mutex(const std::mutex&)’
构建中的整个日志信息如下。
-fPIC -DDS_VERSION="5.0.0" -I /usr/local/cuda-10.2/include -I ../../includes -pthread -I/usr/include/gstreamer-1.0 -I/usr/include/orc-0.4 -I/usr/include/gstreamer-1.0 -I/usr/include/glib-2.0 -I/usr/lib/aarch64-linux-gnu/glib-2.0/include -I/usr/include/opencv4/opencv -I/usr/include/opencv4
g++ -c -o gstdsexample.o -fPIC -DDS_VERSION=\"5.0.0\" -I /usr/local/cuda-10.2/include -I ../../includes -pthread -I/usr/include/gstreamer-1.0 -I/usr/include/orc-0.4 -I/usr/include/gstreamer-1.0 -I/usr/include/glib-2.0 -I/usr/lib/aarch64-linux-gnu/glib-2.0/include -I/usr/include/opencv4/opencv -I/usr/include/opencv4 gstdsexample.cpp
In file included from /usr/include/c++/7/bits/stl_tempbuf.h:60:0,
from /usr/include/c++/7/bits/stl_algo.h:62,
from /usr/include/c++/7/algorithm:62,
from /usr/include/opencv4/opencv2/core/base.hpp:55,
from /usr/include/opencv4/opencv2/core.hpp:54,
from /usr/include/opencv4/opencv2/imgproc.hpp:46,
from /usr/include/opencv4/opencv2/imgproc/imgproc.hpp:48,
from gstdsexample.h:30,
from gstdsexample.cpp:29:
/usr/include/c++/7/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = infos; _Args = {const infos&}]’:
/usr/include/c++/7/bits/stl_uninitialized.h:83:18: required from ‘static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const infos*, std::vector<infos> >; _ForwardIterator = infos*; bool _TrivialValueTypes = false]’
/usr/include/c++/7/bits/stl_uninitialized.h:134:15: required from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const infos*, std::vector<infos> >; _ForwardIterator = infos*]’
/usr/include/c++/7/bits/stl_uninitialized.h:289:37: required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<const infos*, std::vector<infos> >; _ForwardIterator = infos*; _Tp = infos]’
/usr/include/c++/7/bits/stl_vector.h:331:31: required from ‘std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = infos; _Alloc = std::allocator<infos>]’
gstdsexample.cpp:444:40: required from here
/usr/include/c++/7/bits/stl_construct.h:75:7: error: use of deleted function ‘infos::infos(const infos&)’
{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from gstdsexample.cpp:29:0:
gstdsexample.h:66:8: note: ‘infos::infos(const infos&)’ is implicitly deleted because the default definition would be ill-formed:
struct infos{
^~~~~
gstdsexample.h:66:8: error: use of deleted function ‘std::mutex::mutex(const std::mutex&)’
In file included from /usr/include/c++/7/mutex:43:0,
from /usr/include/opencv4/opencv2/core/utility.hpp:62,
from /usr/include/opencv4/opencv2/core.hpp:3291,
from /usr/include/opencv4/opencv2/imgproc.hpp:46,
from /usr/include/opencv4/opencv2/imgproc/imgproc.hpp:48,
from gstdsexample.h:30,
from gstdsexample.cpp:29:
/usr/include/c++/7/bits/std_mutex.h:97:5: note: declared here
mutex(const mutex&) = delete;
在 Struct 中使用的正确方法是什么?
【问题讨论】:
-
您缺少实际发生问题的代码。有些东西试图复制
infos对象,但std::mutex不可复制,因此隐式复制构造函数被删除。错误出现在复制的任何地方,而不是infos结构中。gstdsexample.cpp:444:40: required from here -
我做了编辑。我只有
std::vector<infos> c_infos;里面的头文件。仅在标头中带有互斥锁和向量的结构,我在构建中遇到错误。 -
@batuman 仅此行不会导致此错误。编译器输出指向
gstdsexample.cpp的第444 行。你能展示那条线以及周围的环境吗? -
@cdhowie 谢谢。我已将释放内存添加为
std::vector<infos>(dsexample->c_infos).swap(dsexample->c_infos);。这有什么问题吗? -
@batuman
std::vector<infos>(dsexample->c_infos)复制dsexample->c_infos。我不知道您为什么要从源构造向量的副本,然后将副本与相同的源交换。这对我来说没有任何意义。
标签: c++ multithreading c++11 mutex