Linux C + + cyclic buffer template class

Source: Internet
Author: User

One: Overview

In the actual study and work, we often encounter read and write a large number of data, this time we may use the loop buffer.

Cyclic buffers have great advantages when dealing with large amounts of data, and circular buffers provide a lock-free mechanism on some competitive issues, with the premise that producers and consumers have only one case, otherwise they should be locked.

Second: the implementation of the cyclic buffer theory, such as

Three: The implementation code looks like this:

//CRecycleQueue.h#include<iostream>//loop Buffer class templatetemplate<classT>classcrecyclequeue{Private:        //loop buffer Address pointerT * *_queue; //Loop buffer read cursor (read position)        int_read; //Circular buffer Write cursor (write position)        int_write; //size of the loop buffer        int_size; //Let's call this variable a mask, and then use the bit & to make the loop buffer        int_mask;  Public: Crecyclequeue () {_queue=NULL; _read=0; _write=0; _size=0; _mask=0; }        //initializing the loop buffer        BOOLInitrecyclequeue (intexp) {                        if(0>exp) {                return false; } _read=0; _write=0; //Pass in an integer that shifts the 1//such as Exp = 4//Binary representation of _size:_size =1<<exp; //Binary representation of _mask: 0111_mask = _size-1; //Allocating buffer Space_queue = (T * *)New Char[sizeof(T *) *_size]; if(NULL = =_queue) {                return false; }            return true; }/* * size = + Mask = 0111 * Write or read mask for & operation, can implement loop buffer function * Maybe you'll ask here why not use the% shipped What about the loop function that implements loops? * The answer is that the system & operation efficiency is higher than the% operation efficiency * Push: * write = 0; * 0000 & 0111 = 0; write++ (No. 0 position of write buffer queue) * Write = 1; * 0001 & 0111 = 1; write++ (1th position of write buffer queue) * Write = 2; * 0010 & 0111 = 2; write++ * Write = 3; * 0011 & 0111 = 3; write++ * ... * write = 8; * & 0111 = 0; write++ * Write = 9; * 1001 & 0111 = 1; write++ * ... * * POP: * Read = 0; * 0000 & 0111 = 0; read++ (reads the No. 0 bit data of the buffer queue) * Read = 1; * 0001 & 0111 = 1; read++ (reads the 1th bit data of the buffer queue) * Read = 2; * 0010 & 0111 = 2; read++ * Read = 3 * 0011 & 0111 = 3; read++ * ... * read = 8; * & 0111 = 0; read++ * ... * */                BOOLPush (T *type) {                        if(NULL = =type) {                return false; }            //when the condition is not satisfied, the buffer is full and the data sent in will be lost            if(_write < _read +_size) {                //What we're depositing here is the type pointer, which points to a memory space or class we've allocated ._queue[_write & _mask] =type; _write++; return true; }            return false; } T*Pop () {T*tmp =NULL; //indicates that the buffer has no data when the condition is not satisfied            if(_read <_write) {                //remove data from the queueTMP = _queue[_read &_mask]; _read++; }            returntmp; }                intgetremainsize () {return(_write-_read); }};

The following is a simple test code:

//main.cpp#include<iostream>#include<pthread.h>#include"CRecycleQueue.h"using namespacestd;classuserinfo{Private :        int_num;  Public: UserInfo (intnum) {_num=num; }        intGetusernum () {return_num; }}; Crecyclequeue<UserInfo> *queue =NewCrecyclequeue<userinfo>;void*write_func (void*args) {    intnum =0;  while(1){        //UserInfo can encapsulate the data you want.//here is just a simple test caseUserInfo *info =NewUserInfo (num++); if(!queue->Push (info)) {            //push failed to remove manually allocated memory space            Deleteinfo; } Sleep (1); }}void*read_func (void*args) {     while(1) {UserInfo*info =NULL; if(info = queue->Pop ()) {cout<<info->getusernum () <<Endl; Deleteinfo; } Sleep (1); }}intMain () {queue->initrecyclequeue (8);    pthread_t Pid1;    pthread_t Pid2; //such producers and consumers have only one case, this circular buffer provides a lock-free for competition issues, greatly improving the processing efficiency of the programPthread_create (&pid1,null,read_func,null); Pthread_create (&pid2,null,write_func,null);    Pthread_join (Pid1,null);        Pthread_join (Pid2,null); return 0;}

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

The general functionality of this loop buffer queue has been implemented, with some other operations in the loop buffer queue not being implemented, just a description of some of the core operations!

If there are errors and other comments, put forward to everyone to discuss and learn from each other!

Linux C + + cyclic buffer template class

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.