C # algorithm based on non-lock C # concurrent Queue implementation

Source: Internet
Author: User
Tags bool cas thread advantage

Recently began to learn the lock-free programming, compared with the traditional lock based algorithm, the lock-free programming has its unique advantages, Angel Lucifer of the article on the non-lock programming in detail.

The goal of the lock-free programming is to guarantee the consistency of the shared data in concurrent process without using lock, and its main implementation is the CAS operation, that is, the compare_and_swap, through the instructions provided by the processor, can update the shared data atomically, and simultaneously monitor the interference of other threads. NET in the corresponding implementation is the Interlocked.compareexchange function.

Since lock is not used, it is always to be noted in the lock-free programming that the code may be interrupted in any statement. If it is a single variable, we can use INTERLOCKED.XXX to guarantee the atomic nature of the operation, but if there are multiple operations to complete, simply combining the interlocked.xxx is far from enough. The usual principle is to use the shared variables in the function, first, at the beginning of the code to save its content with a local variable, in the subsequent update of the shared variables, using the aforementioned variables to determine whether it has changed, if the shared variable has changed, then we may need to try again, or, in some cases, the current thread can "help "The thread in the other update completes the update.

From above, we can sum up the two basic features of the unlocked algorithm:

1. The lock-free algorithm always contains a loop structure to ensure that the update fails and then retries

2. The lock-free algorithm always compares the CAs with the original values when updating shared variables to ensure no conflict

The following is a concurrent queue implemented according to the Michael-scott algorithm, in which the dequeue algorithm is described in detail in the article on the IBM Non-blocking algorithm. The code is as follows:

Code

1public class Concurrentlinkedqueue<t>
2{
3 Private Class Node<k>
4 {
5 internal K Item;
6 internal node<k> Next;
7
8 Public Node (K item, node<k> next)
9 {
this. item = Item;
One by one. Next = next;
12}
13}
14
Private node<t> _head;
Private node<t> _tail;
17
Concurrentlinkedqueue Public ()
19 {
_head = new Node<t> (default (T), null);
_tail = _head;
22}
23
public bool IsEmpty
25 {
Num get {return (_head). Next = null); }
27}
28
public void Enqueue (T item)
30 {
node<t> NewNode = new node<t> (item, NULL);
while (true)
33 {
Node<t> curtail = _tail;
node<t> residue = Curtail.next;
36
37//Determine if _tail is changed by other process
if (curtail = = _tail)
39 {
//A has other rocess perform C success, _tail should point to new node
if (residue = null)
42 {
//c other process changes the tail node and needs to be fetched tail node
The IF (interlocked.compareexchange<node<t>> (
Ref curtail.next, NewNode, residue) = = residue)
46 {
//d try to modify tail
Interlocked.compareexchange<node<t>> (ref _tail, NewNode, curtail);
return;
50}
51}
Or else
53 {
//b helps other threads complete D operations
Interlocked.compareexchange<node<t>> (ref _tail, residue, curtail);
56}
57}
58}
59}
60
Trydequeue-BOOL (out T result)
62 {
Curhead node<t>;
Node<t> curtail;
Node<t> Next;
Do
67 {
Curhead = _head;
curtail = _tail;
Next = Curhead.next;
if (Curhead = = _head)
72 {
if (next = null)//queue is empty
74 {
result = Default (T);
The return false;
77}
The (Curhead = = curtail)//queue is in the process of enqueue the first node
79 {
80//Try to help other process to complete the operation
Bayi interlocked.compareexchange<node<t>> (ref _tail, Next, curtail);
82}
Or else
84 {
85//Take next. Item must be placed before CAs
result = next. Item;
87//If _head does not change, point _head to Next and exit
The interlocked.compareexchange<node<t>> (ref _head,
Curhead) = = Curhead)
The break;
91}
92}
93}
while (true);
return true;
96}
97}
98

According to your own test (dual-core CPU), in mild and moderate contention situations, the lock-free algorithm is much better than the one based on the lock, and in the case of very serious contention (100 concurrent threads above/per CPU), the performance of the algorithm based on locks is beginning to show an advantage, because once contention occurs, The lock based algorithm will immediately switch to other threads, and the lock-free algorithm will go into the next cycle, resulting in CPU occupancy. But such serious contention is not seen in practice, and can be improved using spinwait methods. The lock-based algorithm has a deadlock-like phenomenon in the test, the lock-free algorithm does not have a similar problem, in addition, the more processor core, based on the efficiency of the lock algorithm is worse.

From the above algorithm implementation, can realize the advantage of the lock-free algorithm: in multiple threads that are concurrent, the thread is always able to advance, the algorithm can always be completed within a limited number of cycles, and in some cases, a thread can "help" other threads to complete the interrupted work, which has a great effect on improving throughput.

Related Article

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.