Linux c ++ cyclic buffer template class, linux Buffer

Source: Internet
Author: User

Linux c ++ cyclic buffer template class, linux Buffer

I. Overview

In actual learning and work, we often encounter the situation of reading and writing a large amount of data. At this time, we may use a circular buffer.

The circular buffer zone has great advantages in processing a large amount of data. The circular buffer zone provides a lock-free mechanism for some competition issues, if there is only one producer and consumption, otherwise the lock is required.

II. Implementation theory of circular buffer is as follows:

Iii. Implementation Code:

// CRecycleQueue. h # include <iostream> // circular buffer class template <class T> class CRecycleQueue {private: // circular buffer address pointer T ** _ queue; // circular buffer read cursor (read position) int _ read; // circular buffer write cursor (write position) int _ write; // circular buffer size int _ size; // We call this variable as a mask for bitwise & Operation to implement circular buffer int _ mask; public: CRecycleQueue () {_ queue = NULL; _ read = 0; _ write = 0; _ size = 0; _ mask = 0;} // initialize the loop buffer bool InitRecycleQueue (int exp) {if (0> exp) {return false ;} _ Read = 0; _ write = 0; // input an integer to perform a displacement operation on 1. // For example, exp = 4 // The binary representation of _ size: 1000 _ size = 1 <exp; // _ binary representation of mask: 0111 _ mask = _ size-1; // allocate buffer space _ queue = (T **) new char [sizeof (T *) * _ size]; if (NULL = _ queue) {return false;} return true ;} /** size = 1000 mask = 0111 * write or read: Same as mask & operation, you can implement the Circular buffer function * maybe you will ask why the % operation is not used to implement the Circular function? * The answer is that the system and computing efficiency are higher than %. ** Push: * write = 0; * 0000 & 0111 = 0; write ++ (write to the Buffer Queue's 0th position) * write = 1; * 0001 & 0111 = 1; write ++ (write Buffer Queue's 1st position) * write = 2; * 0010 & 0111 = 2; write ++ * write = 3; * 0011 & 0111 = 3; write ++ *... * write = 8; * 1000 & 0111 = 0; write ++ * write = 9; * 1001 & 0111 = 1; write ++ *... ** Pop: * read = 0; * 0000 & 0111 = 0; read ++ (read data at the 0th position of the Buffer Queue) * read = 1; * 0001 & 0111 = 1; read ++ (read data at the 1st position of the Buffer Queue) * read = 2; * 0010 & 0111 = 2; read ++ * read = 3*0011 & 0111 = 3; read ++ *... * read = 8; * 1000 & 0111 = 0; read ++ *... **/bool Push (T * type) {if (NULL = type) {return false;} // when the condition is not met, the buffer is full, the data pushed in will lose if (_ write <_ read + _ size) {// we store the type pointer here, this pointer points to an allocated memory space or class _ queue [_ write & _ mask] = type; _ write ++; return true;} return false ;} T * Pop () {T * tmp = NULL; // if the condition is not met, the buffer has no data if (_ read <_ write) {// retrieve the queue data tmp = _ queue [_ read & _ mask]; _ read ++;} return tmp ;} int GetRemainSize () {return (_ write-_ read );}};

The following is a simple test code:

// Main. cpp # include <iostream> # include <pthread. h> # include "CRecycleQueue. h "using namespace std; class UserInfo {private: int _ num; public: UserInfo (int num) {_ num = num;} int getUserNum () {return _ num ;}; CRecycleQueue <UserInfo> * queue = new CRecycleQueue <UserInfo>; void * write_func (void * args) {int num = 0; while (1) {// UserInfo can encapsulate the data you want. // here is just a simple test case UserInfo * info = new UserInfo (num ++); if (! Queue-> Push (info) {// Push failed to delete the manually allocated memory space delete info;} sleep (1) ;}} void * read_func (void * args) {while (1) {UserInfo * info = NULL; if (info = queue-> Pop () {cout <info-> getUserNum () <endl; delete info;} sleep (1) ;}} int main () {queue-> InitRecycleQueue (8); pthread_t pid1; pthread_t pid2; // when there is only one producer and consumer, this loop buffer provides no lock for competition issues, greatly improving the processing efficiency of the program pthread_create (& pid1, NULL, read_func, NULL); pthread_create (& pid2, NULL, write_func, NULL); pthread_join (pid1, NULL); pthread_join (pid2, NULL); return 0 ;}

Compile: g ++ main. cpp-lpthread-o test

The general function of this circular Buffer Queue has been implemented. Some other operations of the circular Buffer Queue are not implemented, but some core operations are described!

If you have any mistakes or other opinions, I suggest you discuss and learn from each other!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.