Non-lock design of multi-threaded buffer queue in C language

Source: Internet
Author: User

A project developed in the company needs to add multi-threaded buffer queue to the server side, to access data, I am also a novice not too much experience, after collecting a lot of information on the Internet, finally have a set of their own design ideas, and solve the project problems, because at that time, the collection of information, Found online This specific article is not too much or too complex, or too humble, for beginners it is difficult to understand, all I intend to send my design ideas, I hope to help the same classmates and friends, if there is insufficient please guide! Thank you

Project requirements:

Two threads, thread 1 receives the client data and stores it in an orderly buffer queue; Thread 2 extracts the data from the buffer queue and parses the inserted database;

Workaround:

1: Create a new struct Buff_quere, there is an enumeration type inside buffstatus buffer state, empty read-write , and an array type of cached data Buff_up_quere, the received data exists here ; (global)

  

1 /*  */2struct  buff_quere{  3     enum buffstatus {empty,wirte,reads}bufstatus;  // is empty, written, read 4     uint8_t Buff_up_quere[rx_buff_size];            // buffers 5 };
View Code

2: Create a structure array as a buffer, need several to build several; (global)

1 Static structBuff_quere Buff_data1[buffer_queue_len];//Buffer 12 Static intCount_len1 =0;//Buffer 1 Counter3 Static structBuff_quere Buff_data2[buffer_queue_len];//Buffer 24 Static intCount_len2 =0;//Buffer 2 Counter
View Code

3: Thread 1 receives and stores data to buffer;

1 intBUFF_NB =1;//save from first buffer by default2 intNb_next =0;//Next Buffer3 intlen=0;4  while(true)5 {6 //Buffer Queue7Nb_next = buff_nb+1;8 if(Nb_next = =2){9Nb_next =1;Ten } One Switch(BUFF_NB) { A              Case 1: -printf"enter buffer%d for write buffer index=%d\n", Buff_nb,len); -Buff_data1[len].bufstatus =Wirte; thememcpy (&buff_data1[len].buff_up_quere, &BUFF_UP,sizeof(BUFF_UP)); -len++; -                 if(len = =Buffer_queue_len) -                 { +printf"Buffer%d has finished writing", BUFF_NB); -                      while(Count_len2! =0){ +printf"Warning: Buffer%d has not been emptied to stop caching data \ n", nb_next); ASleep1); at                     } -printf", go to buffer%d for caching \ n", nb_next); -Len =0; -BUFF_NB =Nb_next; -                 } -              Break; in              Case 2: -printf"enter buffer%d for write buffer index=%d\n", Buff_nb,len); toBuff_data2[len].bufstatus =Wirte; +memcpy (&buff_data2[len].buff_up_quere, &BUFF_UP,sizeof(BUFF_UP)); -len++; the                 if(len = =Buffer_queue_len) *                 { $printf"Buffer%d has finished writing", BUFF_NB);Panax Notoginseng                      while(Count_len1! =0){ -printf"Warning: Buffer%d has not been emptied to stop caching data \ n", nb_next); theSleep1); +                     } Aprintf", go to buffer%d for caching \ n", nb_next); theLen =0; +BUFF_NB =Nb_next; -                 } $              Break; $             default: -             Continue; -              Break;  the}
View Code

4: Thread 2 obtains data from buffer and manipulate data;

1 intBUFF_NB =1;//gets the buffer number, which defaults to 12 intNb_next =0;//Next Buffer3 intlen=0;4  while(true)5 {6     //Buffer Queue7Nb_next = buff_nb+1;8         if(Nb_next = = One){9Nb_next =1;Ten         }  One         Switch(BUFF_NB) { A              Case 1: -                 if(Buff_data1[len].bufstatus = =Wirte) { -printf"Enter buffer%d to read buffer data index=%d\n", Buff_nb,len); theBuff_data1[len].bufstatus =reads; -memcpy (&buff_up, &buff_data1[len].buff_up_quere,sizeof(Buff_data1[len].buff_up_quere)); -len++; -                     if(len = =Buffer_queue_len) +                     {    -printf"buffer%d has been read index=%d, empty this buffer \ n", Buff_nb,len); +Len =0; ABUFF_NB =Nb_next; atmemset (Buff_data1,0,sizeof(BUFF_DATA1)); -                     } -}Else{ -printf"buffer%d is empty or read, please wait ... \ n", BUFF_NB); -Sleep1); -                     Continue; in                 } -Count_len1 =Len;  to              Break; +              Case 2: -                 if(Buff_data2[len].bufstatus = =Wirte) { theprintf"Enter buffer%d to read buffer data index=%d\n", Buff_nb,len); *Buff_data2[len].bufstatus =reads; $memcpy (&buff_up, &buff_data2[len].buff_up_quere,sizeof(Buff_data2[len].buff_up_quere));Panax Notoginsenglen++; -                     if(len = =Buffer_queue_len) the                     { +printf"buffer%d has been read index=%d, empty this buffer \ n", Buff_nb,len); ALen =0; theBUFF_NB =Nb_next; +memset (BUFF_DATA2,0,sizeof(BUFF_DATA2)); -                     } $}Else{ $printf"buffer%d is empty or read, please wait ... \ n", BUFF_NB); -Sleep1); -                     Continue; the                 } -Count_len2 =Len;Wuyi              Break; the             default: -             Continue; Wu              Break; -}
View Code

Summarize the idea: when thread 1 stores the data to buffer 1, it assigns a write state to each stored element of the buffer, then writes it, and if the buffer 1 is full after the count_len2 is 0 to determine if buffer 2 is empty, the buffer 2 continues to write , otherwise wait;

When thread 2 gets the data from the buffer queue, first determine whether the state of each element in the queue is a write, is to get, not just wait, when the buffer is taken out, and will count_len2=0, tell thread 1 I emptied, you can write!

C language Multi-threaded buffer queue lock-free design ideas

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.