Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why not use lock-free programming in C++?

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "Why do not use lock-free programming in C++". In daily operation, I believe that many people have doubts about why not to use lock-free programming in C++. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "Why not to use lock-free programming in C++". Next, please follow the editor to study!

CP.100: do not use lock-free programming unless absolutely necessary

Reason (reason)

It's error-prone and requires expert level knowledge of language features, machine architecture, and data structures.

This approach is error-prone and requires expert knowledge in language functions, machine architecture, data structures, and so on.

Example, bad (negative example)

Extern atomic head; / / the shared head of a linked list

Link* nh = new Link (data, nullptr); / / make a link ready for insertion

Link* h = head.load (); / / read the shared head of the list

Do {

If (h-> data next = h; / / next element is the previous head

} while (! head.compare_exchange_weak (h, nh)); / / write nh to head or to h

Spot the bug. It would be really hard to find through testing. Read up on the ABA problem.

Find bug. The problems here are really difficult to find through the test. Study the ABA problem carefully.

Exception (exception)

Atomic variables can be used simply and safely, as long as you are using the sequentially consistent memory model (memory_order_seq_cst), which is the default.

Atomic variables can be used simply and safely, as long as you use the Sequential consistent memory Model (memory_order_seq_cst), which is the default premise.

Note (Note)

Higher-level concurrency mechanisms, such as threads and mutexes are implemented using lock-free programming.

Alternative: Use lock-free data structures implemented by others as part of some library.

Advanced concurrency mechanisms, such as threads and mutexes, are implemented through lock-free programming.

Other options: use lock-free programming data structures that exist as part of some libraries implemented by others.

At this point, the study on why unlocked programming is not used in C++ is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report