C/C++ - Ring buffer

以前在项目里用过Ring Buffer,比如用来在中断里存储UART传输的数据,然后在周期任务中再读取数据进行处理。

读取操作和存储操作是独立的,互相不影响。通过游标来表示需要读取和存储数据的位置。

这个功能用C写就是个结构体加操作函数,用C++用一个类就可以。

以前用过的版本还要找一下,网上搜一艘看有没有合适的,现在手头上需要用一下。

cnoviello (Carmine Noviello) · GitHub

这个项目也是十年前的了,不过也是够用了,网上搜下来看,就这个我觉得还行。

​编辑ringbuffer.h687 bytes

​编辑ringbuffer.c1.2 KB


A ring buffer, also known as a circular buffer or circular queue, is a data structure that uses a fixed-size buffer as if it were connected end-to-end, allowing efficient storage and retrieval of data in a first-in, first-out (FIFO) manner.

Here's a more detailed explanation:

Circular Structure:
The key characteristic is that the last element in the buffer is connected to the first, creating a circular appearance.

Fixed Size:
Ring buffers have a predefined size, meaning they don't dynamically resize like some other data structures.

FIFO Behavior:
Data is added (written) and removed (read) from the buffer in a FIFO manner, meaning the first element added is the first element removed.

Efficiency:
Ring buffers are efficient because they avoid the need to shift elements when data is added or removed, which can be time-consuming with standard arrays.

Applications:
Ring buffers are commonly used in situations where data needs to be buffered between different processes or threads, such as in audio/video streaming, network interfaces, and data logging.

Overwriting:
When the buffer is full, new data overwrites the oldest data, ensuring that the buffer always contains the most recent information.

Examples:

  • Network Interface Cards (NICs): Ring buffers store incoming packets until the device driver can process them.
  • Audio/Video Streaming: Ring buffers can store audio/video data packets for smooth playback.
    Data Logging: Ring buffers can store recent log messages, allowing for efficient retrieval of the latest events.
  • Embedded Systems: Due to their fixed size and efficiency, ring buffers are often used in resource-constrained embedded systems.

Circular Buffer | Baeldung on Computer Science

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

夜流冰

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

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

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

打赏作者

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

抵扣说明:

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

余额充值