. Implementation and analysis of parallel programming -3.concurrentqueue in net

Source: Internet
Author: User

In the above. NET in the implementation of parallel programming -2.concurrentqueue and analysis of the concept of non-locking, but also the BCL provided by the Concurrentqueue is based on the implementation of atomic operations, Because Concurrentqueue code is more, this paper mainly analyzes several common operations:

Queue (EnQueue), Outbound (Trydequeue), whether empty (IsEmpty), gets the number of elements in the queues (count).

First, CONCURRENTQUEUE internal structure:

1. Principle of implementation

It is well known that there are two implementations of the normal non-thread-safe queue:

1. Loop queue implemented using arrays.

2. Queues implemented with linked lists.

Let's look at the pros and cons of two ways:

The implementation of the normal queue in. Net Farmework uses the first approach, with the drawback that when the queue space is not sufficient to expand, the main implementation of the expansion is to open a new array of twice times the original length, and then copy the data inside the original array into the new array. Therefore, when the capacity of the expansion will generate a small memory overhead, in the concurrency of the performance of the environment can not be underestimated. Of course, when you call the queue's constructor, you can specify the size of the default space, but in general, the amount of data is unpredictable, the choice of large will be a space wasted, the choice of small will have the cost of replication memory, and the queue expansion after the need to display call TrimToSize () method to reclaim unused memory space.

The second type of implementation of the list, although the problem of the elimination of space waste, but also increased the pressure of the GC, when the queue will be assigned a new node, when the team to discard the node, for a large number of teams out of the queue operation when the implementation of the performance is not high.

Combining the two implementations, Concurrentqueue uses the concept of segmented storage (as shown) when supporting multithreading and issuing teams to queue concurrently, and Concurrentqueue allocates memory in segments (Segment). A segment contains an array with a default length of 32 and a pointer to the next segment, with the head and tail pointers pointing to the starting and ending segments, which are somewhat like the section memory management of the operating system and the page-memory management strategy. This implementation of allocating memory not only relieves the pressure of the GC, but also the caller does not have to show the call to the TrimToSize () method to reclaim the memory (when a piece of memory is empty, the memory is reclaimed by the GC).

2.Segment (segment) Internal structure

In fact, for the concurrentqueue operation is actually the segment (data section) operation.

Segment can abstract the following data structures:

Segment Internal Main method:

Segment internal and the ordinary queue implementation of the equivalent, but for the team and out of the operation of the use of atomic operations to prevent multi-threaded competition, the use of random concessions and other techniques to ensure a live lock and other issues, the implementation mechanism and Concurrentstack difference is not small, With the implementation details of multi-tryappend in the source comments have been elaborated very clearly here to do not too much explanation.

Second, the queue operation

As shown, the queued operation is performed in the trailing segment, and when the data enters the segment, a fallback operation is performed and then the attempt is made to succeed, the reason for the failure (tail. Append (item) returns false) only one is when a new segment is being allocated when there is not enough space in the paragraph, and the elements that will enter the segment will fail during that period.

Third, the team operation

As shown, the queue fails to return false instead of being queued as a fallback operation because only one of the reasons for the team failure is when the elements of all segments within the queues are empty, so the team is designed to return a bool value function.

Iv. Judging whether it is empty (IsEmpty)

The complexity of the Whole judgment O (1) is mainly in three cases:

1. The head node (segment) is not empty returns false

2. The head node is empty and the next node is NULL returns True

3. The head node is empty and the next node is not NULL returns FALSE, this situation indicates that the queue is expanding, so you have to choose to wait for the expansion is complete again to judge

V. Get the number of elements in the queue (count)

Locate the low position of the head node and the high position of the tail node, since each segment records the current segment's index in the queue, it is easy to find the number of elements in the entire queue.

Same as Concurrentstack. Microsoft Official documentation and comments also indicate: Determine if the queue is empty to use the IsEmpty property instead of judging by the Count = = 0 The reason is that getheadtailpositions in the process of large amounts of data queued and out of the team to find the location of the node is more time-consuming operation, to constantly cycle the position of the tail and the beginning and ends, so determine whether the queue is empty or use the IsEmpty property.

. Implementation and analysis of parallel programming -3.concurrentqueue in net

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.