Circular Buffer

I recently needed a class that stored a character array as a circular buffer. If you are unfamiliar with circular buffer, it is a permanently allocated buffer that has both a read and write position. I have done a few of them in the past, but had lost all of my examples. So I wrote a new one and placed it into a class called CircularBuffer. The CircularBuffer can be read from and written to, and it has methods to get the read and write count. As the buffer is written to the write position increases til the end of the buffer is reached, at that point the write position is wrapped back to the start of the buffer and writing starts from there. The same happens to the read operation also, but the read position is incremented. It is important to note that the read position will never be greater than the write positon, since if it were we would be reading data that had not been written to the buffer. Likewise, the write position will never wrap the read position otherwise data that had not been read would be overwritten.

The basic operations are read and write, both fail if an overlap condition could occur.

To get the number of availible characters to read the readCount method is used. If the count is greater than zero use the read method to get the latest contents written into the buffer.

I have included a small test application in a project TestQue. The test driver is in the TestQue.cpp. It is multithreaded win32 console app developed using VC6. The CircularBuffer code is in CircularBuffer.cpp and h.

Downloads

Download demo project - 12 Kb


Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • IBM Worklight is a mobile application development platform that lets you extend your business to mobile devices. It is designed to provide an open, comprehensive platform to build, run and manage HTML5, hybrid and native mobile apps.

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds