最近在做图像采集——》然后进入算法——》接着再显示到屏幕,这样就出现了问题,硬件不断的采集图像上传上来,然而算法的速度不够快,就出现 了问题。
如果只开辟一个内存来读取写入,那么,就只能给上锁,这样会降低效率。出现卡顿的情况
,还有一种问题就是采集慢,算法快也会出现卡顿。
所以就写了一个三缓存机制来应对。
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();
}
程序运行结果: