Ping Pong Buffer 双缓冲 C++代码学习

1、Ping Pong Buffer 原理分析

双缓冲

基本原理如上图所示,当设备有数据来时,先放入缓冲区1

在这里插入图片描述

然后将缓冲区1的数据放入缓冲区2,这时缓冲区1可接收下次数据。工作区可从缓冲区2拿数据

2、C++代码实现

相关结构体创建

typedef struct
    {
        void *buffer[2];
        volatile uint8_t writeIndex;
        volatile uint8_t readIndex;
        volatile uint8_t readAvaliable[2];
    } PingPongBuffer_t;

void *buffer[2] 无类型指针,指向不同数据类型的缓冲Buff

volatile uint8_t writeIndex 缓冲写索引

volatile uint8_t readIndex 缓冲读索引

volatile uint8_t readAvaliable[2] 可读标志位

相关函数实现

/**
   * @brief  Ping-pong buffer initialization
   * @param  ppbuf: Pointer to the ping-pong buffer structure
   * @param  buf0:  Pointer to the first buffer
   * @param  buf1:  Pointer to the second buffer
   * @retval None
   */
void PingPongBuffer_Init(PingPongBuffer_t *ppbuf, void *buf0, void *buf1)
{
    memset(ppbuf, 0, sizeof(PingPongBuffer_t));
    ppbuf->buffer[0] = buf0;
    ppbuf->buffer[1] = buf1;
}

/**
  * @brief  Get a readable buffer
  * @param  ppbuf:     Pointer to the ping-pong buffer structure
  * @param  pReadBuf:  Pointer to the pointer to the buffer to be read
  * @retval Returns true if there is a buffer to be read
  */
bool PingPongBuffer_GetReadBuf(PingPongBuffer_t *ppbuf, void **pReadBuf)
{
    if (ppbuf->readAvaliable[0])
    {
        ppbuf->readIndex = 0;
    }
    else if (ppbuf->readAvaliable[1])
    {
        ppbuf->readIndex = 1;
    }
    else
    {
        return false;
    }
    *pReadBuf = ppbuf->buffer[ppbuf->readIndex];
    return true;
}

/**
  * @brief  Notify buffer read completion
  * @param  ppbuf: Pointer to the ping-pong buffer structure
  * @retval None
  */
void PingPongBuffer_SetReadDone(PingPongBuffer_t *ppbuf)
{
    ppbuf->readAvaliable[ppbuf->readIndex] = false;
}

/**
  * @brief  Get writable buffer
  * @param  ppbuf:      Pointer to the ping-pong buffer structure
  * @param  pWriteBuf:  Pointer to the pointer to the buffer to be wriye
  * @retval None
  */
void PingPongBuffer_GetWriteBuf(PingPongBuffer_t *ppbuf, void **pWriteBuf)
{
    if (ppbuf->writeIndex == ppbuf->readIndex)
    {
        ppbuf->writeIndex = !ppbuf->readIndex;
    }
    *pWriteBuf = ppbuf->buffer[ppbuf->writeIndex];
}

/**
  * @brief  Notify buffer write completion
  * @param  ppbuf: Pointer to the ping-pong buffer structure
  * @retval None
  */
void PingPongBuffer_SetWriteDone(PingPongBuffer_t *ppbuf)
{
    ppbuf->readAvaliable[ppbuf->writeIndex] = true;
    ppbuf->writeIndex = !ppbuf->writeIndex;
}

  • 0
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Pingpong Buffer, 或者称为Ping-Pong缓冲区,是一种用于在两个线程之间传输数据的高效方法。它使用两个缓冲区,一个用于写入数据,一个用于读取数据。当一个线程完成数据写入后,另一个线程可以开始读取数据,这样两个线程可以并行执行,提高了系统的效率。 下面是一个简单的Pingpong Buffer代码实现: ```python from threading import Lock class PingPongBuffer: def __init__(self): self.buffer1 = [] self.buffer2 = [] self.current_buffer = 1 self.lock = Lock() def write(self, data): with self.lock: if self.current_buffer == 1: self.buffer1.append(data) else: self.buffer2.append(data) def read(self): with self.lock: if self.current_buffer == 1: data = self.buffer2.pop(0) self.current_buffer = 2 else: data = self.buffer1.pop(0) self.current_buffer = 1 return data # 在一个线程中写入数据 def write_data(buffer, data): buffer.write(data) # 在另一个线程中读取数据 def read_data(buffer): data = buffer.read() print("Read data:", data) # 创建Pingpong Buffer实例 buffer = PingPongBuffer() # 创建两个线程并启动 import threading threading.Thread(target=write_data, args=(buffer, "Hello")).start() threading.Thread(target=read_data, args=(buffer,)).start() ``` 上述代码使用Python中的多线程模块,实现了Pingpong Buffer的基本功能。其中,`write`方法用于向当前缓冲区写入数据,`read`方法用于从另一个缓冲区读取数据。通过在不同线程中调用这两个方法,实现了数据的并行交换。 需要注意的是,为了避免数据竞争条件,我们使用了线程锁 (`Lock`) 来确保每个线程对缓冲区的访问是互斥的。当一个线程正在写入数据时,另一个线程必须等待执行读取操作,反之亦然。 这只是一个简化的示例,实际的Pingpong Buffer实现可能会更加复杂,需要根据具体的应用场景进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值