Readerwriterlock and lockfree Data Structure in C #

Source: Internet
Author: User
A while ago, readerwriterlock was used in a project and two problems were found:
    1. Poor performance
    2. Upgradetowriterlock does not convert atomic from readerlock to writerlock, but is equivalent to "lock. releasereaderlock (); lock. acquirewriterlock ();". This kind of semantics is quite confusing. At the beginning, I thought this operation was Atomic. It took a long time for the bug to occur and debug it. However, after careful consideration, I found that this is not actually the fault of. Net designer, and there is no way to design this operation into atomic. The reason is as follows:
  • Multiple Threads simultaneously acquire to readerlock,
  • All of them call upgradetowriterlock. If the operation is atomic, no thread can upgrade successfully.

Later, I simply didn't need to use readerwriterlock and changed it to the lockfree method. This benefits some IBM paper. Http://www.research.ibm.com/people/m/michael/pubs.htm
)
Implementing lockfree in C # is actually very simple, because with the garbage collection,
Code: 1 Class Lockfreedictionary < Key, Value >
2 {
3 Private dictionary < Key, Value > M_dict =   New Dictionary < Key, Value > ();
4
5 Public Value Lookup (Key key)
6 {
7 Return M_dict [Key];
8 }
9
10 Public   Void Update (Key key, value)
11 {
12 Dictionary < Key, Value > Newdict =   Null ;
13 Dictionary < Key, Value > Olddict =   Null ;
14 Do
15 {
16 Olddict = M_dict;
17 Newdict =   New Dictionary < Key, Value > (Olddict );
18 Newdict [Key] = Value;
19 } While (Interlocked. compareexchange < Dictionary < Key, Value > ( Ref M_dict, newdict, olddict) ! = Olddict );
20 }
21 }
22

Explanation:

    1. Line 16, keep a reference to the original dictionary object,
    2. Line 17, construct a new dictionary object base on original object. For olddict, this step is readonly, and doesn't need lock,
    3. Line 18, perform the update operation upon the new constructed object,
    4. Line 19, try to swap the new object into the original one. if the return value of interlocked. compareexchange operation is not equal to olddict, it means during this Do-while block executation, there is another thread changed m_dict. in this scenario, we need to do the update again.
    5. The swapped out object (olddict) can be collected by garbage collection.
    6. If we want to use lockfree Data Structure in C ++, there is another technique called hazard pointer. It's in the IBM research papers.

However, this lockfreedictionary can be used in no case. Otherwise, you will get the opposite effect (poor performance). Here, scenario has a lot of read and write operations. However, this situation is quite common.

The advantage of this method is that there is no lock during lookup, which greatly improves performance. (In my project, it is 2000 times higher than readerwriterlock ,)

If you have any research or are interested in lockfree, please leave a message for discussion ,:)

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.