Lock free technology in parallel programming

Source: Internet
Author: User

Lock free (Chinese characters are generally referred to as "no lock" and generally refer to the CAS instruction-based lock-free technology) is to use some special atomic commands of the processor to avoid lock in traditional parallel design).

As we all know, locks may introduce many new problems while solving resource access problems in parallel processes, such as deadlocks ), in addition, the lock application/release has a significant impact on the performance. Of course, the biggest problem is that it is difficult to combine the code modules using the lock. The goal of lock free is to eliminate the adverse effects of lock on programming.

C ++ Daniel Andrei Alexandrescu (the GG who played the template well, author of modern c ++ design) the article "lock-free data structures" is the masterpiece of lock free. If you are interested, you 'd better take a look at it. (BTW, Andrei seems to be playing something BT, haha ).

However, lock free itself is also a controversial option among various parallel solutions: this technology is a bit too strange and difficult to grasp, but on the other hand, because it is completely based on the most basic programming technology, it does not depend on any language/platform. Theoretically, it can be widely used.

In terms of parallel programming, functional things (such as Erlang and Haskell) can be considered a different solution, while lock free can be regarded as a local solution. Therefore, there are no conflicts between various solutions. They all serve the people ;)

My personal opinion on lock free is that this technology should not and will not be applied in a wide area in actual programming. After all, such difficult things are still a bit of a high level. The technology itself is wise and wise. You can use it if you love it. You don't need to drop it down. However, whether or not I want to use the lock free technology in practice, understanding and researching this technology itself will be of great help to understand parallel programming.

The foundation of lock free is the CAS (compare and swap) function, which can be described using the following code:

Template <class T> <br/> bool CAS (T * ADDR, t expected, T value) {<br/> If (* ADDR = expected) {<br/> * ADDR = value; <br/> return true; <br/>}< br/> return false; <br/>}

If you have never really understood the lock free technology before, you may be confused. How can this function help us solve the competition problem in parallel? If you have such a question, it's okay, because the first time I saw this, it was also confused. However, this function only describes the execution process of compare and swap.NoIt is used directly. It is just a description of pseudo code. Remember. However, modern processors generally implement atomic commands corresponding to CAS functions. For example, the "cmpxchg" in x86 Assembly provides such functions. Therefore, the implementation of CAS is actually platform-related. The article provides the implementation of Using ASM in Linux. In contrast, the implementation on windows can be simpler, because Windows provides a family of Apis starting with interlockedcompareexchange to encapsulate specific implementations, for example, the interlockedcompareexchange function declaration is as follows:

Long

Interlockedcompareexchange (

In out plong destination,

In long exchange,

In long comparand

);

It is worth noting that the return value of the function is the original * destination content. Instead of directly returning a Boolean value as described in the C ++ code above, it indicates whether the exchange operation actually happens. Therefore, the return value must be done by ourselves. Obviously, we cannot compare * destination and comparand at the beginning of the function, and then use a branch such as if/else to choose whether to return true or false, in this case, a lock is required for protection. It is difficult for us to find a way to avoid lock dependencies. Can we turn around again? The standard practice here is to use the returned values of API calls to compare with the input parameters of comparand, because the API ensures that a long value object is returned and this value always exists on the function stack, therefore, even if the comparison is interrupted and the actual * destination content is modified by other threads, the comparison result is not affected. Of course, the return value of the CAS function is not affected. The code is implemented as follows:

Template <class T> <br/> bool CAS (T * ADDR, t expected, t fresh) {<br/> return expected = (t) interlockedcompareexchange (long *) ADDR, (long) Fresh, (long) expected); <br/>}< br/>

Of course, interlockedcompareexchangepointer can be directly used to avoid display type conversion when T is usually a pointer in actual calls. You can add a special template, but the processing method is the same.

Recently I found an article in codeproject that uses C ++ and C # To implement the lock free algorithm, but unfortunately this implementation is problematic. It can also be explained that parallel programming, especially lock free, is indeed not an easy task. even such an article is wrong. Let's take a look at its CAs function implementation:

Bool CAS (pointer_t & DEST, pointer_t & compare, pointer_t & Value) <br/>{< br/> If (compare. PTR = interlockedcompareexchangepointer (pvoid volatile *) & DeST. PTR, value. PTR, compare. PTR) {<br/> interlockedexchange (& DeST. count, value. count); <br/> return true; <br/>}< br/> return false; <br/>}< br/>

The problem here is that the function uses two statements to modify the target object. Although both statements are atomic, they may still be interrupted, therefore, this cas function does not play the expected role. In fact, the essence of the lock free Technology Based on cas statements is to modify a copy of the target object instead of directly modifying the object itself ), then, the modified copy content is assigned to the target object through an exchange operation for atomic. Therefore, CAS statements are usually used as follows:

Do {<br/> polddata = pdest; <br/> Delete pnewdata; <br/> pnewdata = pdest-> copy (); <br/> // modify pnewdata <br/>} while (! CAS (& pdest, polddata, pnewdata) <br/>

In fact, similar ideas are also applied to solve other problems. If you haven't thought of anything, you can go to the smart pointer code in C ++ to check it out, so this is actually exception safe) encoding is one of the essential weapons.

The above copy operation is relatively inefficient, so Niu came up with various methods in specific applications to reduce the granularity of data copy. However, in any case, it is incorrect to implement the CAS statement into multiple operations that require reading and writing the original DEST data. Here, a feasible method is to use an enhanced cas api similar to interlockedcompareexchange64 stream. There are still many clever solutions to the specific implementation, which are beyond the scope of this article.

Off-topic: IfAbnormal Security(Exception safety) encoding. It may be found that the lock free mentioned in this article is similar. Because in exceptional security, the best way to modify resources is not to directly modify the target object itself, but to create/modify a copy object first, finally, modify the content of the target object by ensuring that no exception is thrown. If you do not know, you can find a smart pointer (suchBoostInsideShared_ptr) Source code l check whether it has used STD: swap () many times ().

Lock free is used to ensure thread access security. Exception safety is used to ensure data security when an exception is thrown. However, the basic idea for solving the problem is the same.

 

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.