Concurrent lock-Free queue Learning (single-producer single-consumer model)

Source: Internet
Author: User

1. Introduction
This article describes the queues for single-producer single-consumer models. According to the content of the write queue is fixed length or variable length, divided into single-producer single-consumer fixed-length queue and single-producer single-consumer variable length queue two kinds. The single-producer single-consumer model of the queue operation process is not required to be locked. The producer controls the queued operation through the write index, and the consumer controls the queue operation by reading the index. The two are exclusive to the index, and there is no competitive relationship. As shown in the following:

2. Single-producer single-consumer fixed-length queue

This queue requires that the contents of each queue and out-of-team are fixed-length, that is, the producer writes the queue and the consumer reads the contents of the queues with the same thing. The Kfifo in the Linux kernel is such a queue that provides two indexes for reading and writing. A single-producer single-consumer queue data structure is defined as follows:
  

typedefstruct{    /*读指针*/    /*写指针*/    uint32_t size;    /*缓冲区大小*/    char *buff[0];    /*缓冲区起始地址*/}ring_buff_st;

To facilitate the calculation of the location, set the size of the queue to a power of 2. This converts the previous fetch operation to a bitwise operation, that is, R_index = r_index% size is equivalent to R_index = R_index & (size-1). The bit operation is very fast and takes full advantage of the binary features.
(1) The initial state of the queue, the read and write indexes are equal, and the queue is empty.

(2) Write queue

Write operations are queued, there are three kinds of scenes,

2.1 Write index greater than equals read index


2.2 Write index is less than read index

2.3. Writing an index is not enough to write a

(3) Read queue

Read queue divided into three different scenarios

3.1 Write index greater than equals read index

3.2 Write index is less than read index

3.3. There is not enough to read an index

3. Single-producer single-consumer variable-length queue

Sometimes the length of the data written by the producer is indeterminate, causing the data to be written to the queue to be long time-varying. In order to make full use of the queue, an end index is added to ensure that at least one data is written to the end of the queue. The variable-length queue data structure is defined as follows:
See my blog for details about edge length arrays

http://blog.csdn.net/xy010902100449/article/details/46522533
 

typedefstruct{    /*读指针*/    /*写指针*/    /*队列结束指针*/    uint32_t size;    /*缓冲区大小*/    char *buff[0];    /*缓冲区起始地址*/}ring_buff_st;

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Concurrent lock-Free queue Learning (single-producer single-consumer model)

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.