Ring Buffer (circular Buffer)环形缓冲区简介 (四)

2014-11-24 02:34:43 · 作者: · 浏览: 18
个元素。这个方法会锁住Mutex然后等待,直到有一个未读的元素进入缓冲区。 假如至少有一个未读元素的时候,这个方法就会减少未读元素的数量,然后从circular_buffer中读取一个未读元素。然后就解锁Mutex,并通知等待中的一个生产者线程,告诉它又新的空间可以插入新元素了。

3. 这个 pop_back() 方法移除元素,元素仍然留在 circular_buffer 里,这样的话,当circular_buffer满的时候它就会被生产者线程用一个新的元素替代。这个技术比移除一个元素更有效率。


下面的网址是一个环形缓冲区的 C++ 实现,可以用来处理二进制数据,改天有空了翻译一下,方便大家阅读

Circular Buffer of Raw Binary Data in C++

Circular Buffer, Cyclic Buffer or Ring Buffer is a data structure that effectively manages a queue of some items. Items can be added at the back and removed from the front. It has limited capacity because it is based on preallocated array. Functionality is implemented using two pointers or indices - pointing to the first and past the last valid element. The Begin pointer is incremented whenever an item is popped from the front so that it "chases" the End pointer, which is incremented whenever a new item is pushed to the back. They can both wrap around the size of the array. Both operations are done very effectively - in constant time O(1) and no reallocations are needed. This makes circular buffers perfect solution for queues of some data streams, like video or audio.

It's not very sophisticated data structure, but there is one problem. Sample codes of circular buffers you can find on the Internet, just like for many other data structures, operate usually on a single object of some user-defined type. What if we need a buffer for raw binary data, stored as array of bytes We can treat single bytes as data items, but enqueueing and dequeueing single bytes with separate function calls would not be efficient. We can, on the other hand, define some block of data (like 4096 bytes) as the type of item, but this limits us to operating on on such block at a time.

Best solution would be to write an implementation that operates on binary data in form of (const char *bytes, size_t byte_count) and allows writing and reading arbitrary amount of data in a single call, just like functions for writing and reading files do. The only problem that arises in such code is that sometimes the block of data you want to write to or read from the buffer is not in a continuous region of memory, but wraps around to the beginning of the array so we have to process it on two parts - first at the end of the array and the second at the beginning.

Here is my C++ implementation of a circular buffer for raw binary data:

#include // for std::min

class CircularBuffer
{
public:
CircularBuffer(size_t capacity);
~CircularBuffer();

size_t size() const { return size_; }
size_t capacity() const { return capacity_; }
// Return number of bytes written.
size_t write(const char *data, size_t bytes);
// Return number of bytes read.
size_t read(char *data, size_t bytes);

private:
size_t beg_index_, end_index_, size_, capacity_;
char *data_;
};

CircularBuffer::CircularBuffer(size_t capacity)
: beg_index_(0)
, end_index_(0)
, size_(0)
, capacity_(capacity)
{
data_ = new char[capacity];
}

CircularBuffer::~CircularBuffer()
{
delete [] data_;
}

size_t CircularBuffer::write(const char *data, size_t bytes)
{
if (bytes == 0) return 0;

size_t capacity = capacity_;
size_t bytes_to_write = std::min(bytes, capacity - size_);

// Write in a single step
if (bytes_to_write <= capacity - end_index_)
{
memcpy(data_ + end_index_, data, bytes_to_write);
end_index_ += bytes_to_write;
if (end_index_ == capacity) end_index_ = 0;
}
// Write in two steps
else
{
size_t size_1 = capacity - end_index_;
memcpy(data_ + end_index_, data, size_1);
size_t size_2 = bytes_to