Implementation of lock-free map

Source: Internet
Author: User
Tags ibm developerworks faa

Some time ago I read about the lock-free map implementation technology, mainly using CAS, FAA and so on.

If you do not know CAS and FAA, you can go to Baidu. CAS and FAA are more efficient than locks because they use the CPU features and do not need to consider the memory context during switching.

Here we come up with a non-lock map implementation idea. We can inherit the map from STL and encapsulate it by ourselves. The CAS technology is used to implement the lock mechanism and insert the encapsulated map, delete and other operations are synchronized using the lock mechanism implemented by CAS.

Due to my use of vs2008, stl cas implementation cannot be provided (cross-platform). Now, implementation in Windows is provided.

# Include "windows. H "class caslock {const long klockedflag _ = 1; const long kunlockflag _ = 0; void lock () {While (true) {If (interlockedcompareexchange (Lock _, klockedflag __, kunlockflag _) = kunlockflag _) {break;} Sleep (1) ;}} void unlock () {lock _ = kunlockflag __;} PRIVATE: // The value of lock _ 1 indicates that someone occupies the lock. The value of lock_1 0 indicates that volatile long lock _;} is not preemptible currently _;};

Of course, the above is just a simple implementation. If you need to do a lock-free queue and you need to consider other issues, we will not go into detail here. You can worship some papers.

Code project's Xiongwen 《Yet another implementation
Of a lock-free circular array queue"

Herb Suter's 《Writing lock-free code:
Corrected queue"-Use the STD: Atomic template of C ++ 11.

IBM developerworks 《Design a concurrent data structure without mutex lock"



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.