5

我正在尝试为我的程序实现一个循环缓冲区。缓冲区用于在两个线程之间共享数据,如下所示。我使用 OpenCV 从相机中抓取视频帧(线程 1)。然后我想将这些数据存储在一个循环缓冲区中,以便线程 2 可以从缓冲区中获取数据。

在此处输入图像描述

如何cv::Mat在 C++ 中为对象实现循环缓冲区?我知道如何为标准 C++ 对象(如intor char)创建循环缓冲区,但我无法使其与类型的对象一起使用cv::Mat

有什么建议么?

4

3 回答 3

5

已解决,请参阅 循环缓冲区的线程安全实现

于 2012-02-29T15:18:02.070 回答
3

Whats wrong with just a vector and an index to the next slot to write to and the next one to process?

All you have to handle is the wrap around when you get to the end, and if you use a power of 2 in the vector size you can use a simple mask for that.

于 2012-02-27T22:54:20.693 回答
1

当只有写入线程更新end指针并且只有读取线程更新start指针并且对这些指针的访问是原子的时,循环缓冲区是线程安全的。cbWrite您在该更新中占有一席之地,start这将导致竞争条件。

于 2012-02-27T23:06:15.353 回答