循环缓冲区C++的一种实现

本文介绍了在C++中实现循环缓冲区的方法,基于循环队列思想,详细给出了头文件CircleBuffer.h的内容,并通过在Ubuntu OS上进行简单测试,验证了其在多线程环境下(读写线程)的正确性。测试结果显示缓冲区长度、读写位置及字符顺序打印均符合预期。
摘要由CSDN通过智能技术生成

之前去腾讯面试的时候被问到的一道题目:实现一个循环缓冲区(不带互斥锁)。仔细一想,其实和循环队列的思想一模一样,还是怪自己数据结构没学好阿(其实我是学通信的,所以最近在恶补)。还是先上代码

头文件如下,CircleBuffer.h

#ifndef _CIRCLE_BUFFER_H_
#define _CIRCLE_BUFFER_H_

//basic data type rename
typedef unsigned char uchar8;
typedef char char8;
typedef unsigned int uint32;
typedef int int32;

//definition of CircleBuffer
class CircleBuffer
{
public:
	CircleBuffer(int sz);//constructor
	~CircleBuffer();//destructor
	void ClearBuffer();//clear the buffer to init state, just modify the indicator
	bool IsBufferFull();//to judge if the buffer is full
	bool IsBufferEmpty();//to judge if the buffer is empty
	uint32 BufferLength();//calculate buffer's length
	uint32 ReadBuffer(uchar8 *data, uint32 len);//read data from the buffer, return the actual data length
	uint32 WriteBuffer(uchar8 *data, uint32 len);//write data into the buffer, return the actual written-in da
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值