ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C ++线程(安全数据结构)

2022-10-18 12:03:26  阅读:177  来源: 互联网

标签:CC++|(预处理详解) C++ 数据安全 结构


如何解决C ++中的线程安全数据结构?
就像使线程安全的C ++数据结构一样。


struct infos{
int crowdinfos[10][horGridNums*verGridNums];
int cameraSourceID;
static int idx;
std::mutex mutex;

};
mutex.lock和解锁将用于线程安全。

编辑: 在我的头文件中,我将获得一个信息向量。


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,std::vector<infos> >; _ForwardIterator = infos*]’
/usr/include/c++/7/bits/stl_uninitialized.h:289:37: required from ‘_ForwardIterator std::__uninitialized_copy_a(_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 gstdsexample.cpp:29:
/usr/include/c++/7/bits/std_mutex.h:97:5: note: declared here
mutex(const mutex&) = delete;
在Struct内部使用的正确方法是什么?

解决方法
由于您还没有完全发布代码,所以我不确定您的问题是什么。因此,请改用下面的示例。

这是我将互斥量用于队列的方式。通过查看它,您可以使自己的结构适应您想要完成的任务。


class Queue {
public:
Queue() = default;

/**
* Push a message to the queue.
*/
void push(const std::array<int,4>& message) {
while (true) {
std::unique_lock<std::mutex> locker(mu);
cond.wait(locker,[this](){ return buffer_.size() < size_; });
buffer_.push_back(message);
locker.unlock();
cond.notify_all();
return;
}
}

/**
* Pop a message off the queue.
*/
bool pop(std::array<int,4>& value) {
while (true) {
std::unique_lock<std::mutex> locker(mu);
if (buffer_.size() == 0) {
return false;
}
value = buffer_.front();
buffer_.pop_front();
locker.unlock();
cond.notify_all();
return true;
}
}

private:
std::mutex mu;
std::condition_variable cond;

std::deque<std::array<int,4>> buffer_;
const unsigned int size_ = 200;

};
简而言之,只要您希望线程安全地访问某些对象,就可以执行以下操作:


std::unique_lock<std::mutex> locker(mu);

//Add Your code on the object you want thread safe access to.

locker.unlock();
cond.notify_all();
重要的是,您如上所述只能在更衣室/解锁中访问要线程安全访问的对象,并如上所述通知所有等待该对象的人。

请查看您的错误消息:


‘infos::infos(const infos&)’ is implicitly deleted because the default definition would be ill-formed: struct infos{

标签:CC++|(预处理详解),C++,数据安全,结构
来源:

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有