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{

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-