关于三缓存机制的使用

98 篇文章 5 订阅

最近在做图像采集——》然后进入算法——》接着再显示到屏幕,这样就出现了问题,硬件不断的采集图像上传上来,然而算法的速度不够快,就出现 了问题。

 

如果只开辟一个内存来读取写入,那么,就只能给上锁,这样会降低效率。出现卡顿的情况

,还有一种问题就是采集慢,算法快也会出现卡顿。

所以就写了一个三缓存机制来应对。

triplebuf.h文件,也是核心文件是一个类模板

然后定义一个test.h的类来测试

#ifndef TEST_H
#define TEST_H
#include <QObject>
#include "triplebuff.h"
struct FrameStruc{
    int fd_raw;
};
class test: public QObject
{
public:
    test();
    ~test();

    void add_foo();

private:
    FrameStruc                      m_struct_frame[3];     // 三缓存区
    SciPack::TripleTpl<FrameStruc*>  m_triple_obj;        // 三缓存对象
};

#endif // TEST_H

test.cpp

#include "test.h"
#include <thread>
#include <iostream>
#include <cassert>
#include <atomic>
#include <vector>
#include <unistd.h>
#include <QThread>
#include <sstream>
#include <QDebug>
void test::add_foo()
{
    FrameStruc s;
    s.fd_raw = 4;
    FrameStruc* struc = ((&m_triple_obj)->readValue( ));//读取
    qDebug()<<struc->fd_raw;//打印
    (&m_triple_obj)->swap();//交换
    struc = ((&m_triple_obj)->readValue( ));//读取
    qDebug()<<struc->fd_raw;//打印
    (&m_triple_obj)->swap();//交换
    struc = ((&m_triple_obj)->readValue( ));//读取
    qDebug()<<struc->fd_raw;//打印
    (&m_triple_obj)->writeValue(&s);//写入
    (&m_triple_obj)->swap();//交换
    struc = ((&m_triple_obj)->readValue( ));//读取
    qDebug()<<struc->fd_raw;//打印
    (&m_triple_obj)->swap();//交换
    struc = ((&m_triple_obj)->readValue( ));//读取
    qDebug()<<struc->fd_raw;//打印
}

test::test():
    m_triple_obj( & m_struct_frame[0], & m_struct_frame[1], & m_struct_frame[2] )
{
    m_struct_frame[0].fd_raw = 1;
    m_struct_frame[1].fd_raw = 2;
    m_struct_frame[2].fd_raw = 3;
    add_foo();
}

test::~test()
{

}

main.cpp

#include <QApplication>
#include "test.h"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    test sl;
    return a.exec();

}


程序运行结果:


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vqt5_qt6

你的鼓励是我们创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值