如何编写无锁结构?

编程入门 行业动态 更新时间:2024-10-22 02:56:38
本文介绍了如何编写无锁结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的多线程应用程序中,我看到其中有大量锁争用,这妨碍了跨多个内核的良好可伸缩性.我决定使用无锁编程来解决此问题.

In my multithreaded application and I see heavy lock contention in it, preventing good scalability across multiple cores. I have decided to use lock free programming to solve this.

如何编写无锁结构?

推荐答案

简短的答案是:

你不能.

长答案是:

如果您问这个问题,您可能不了解足以创建无锁结构的知识.创建无锁结构非常困难,只有该领域的专家才能做到.无需编写自己的代码,而是搜索现有的实现.当您找到它时,检查它的使用范围,记录的程度,是否得到充分证明,有什么限制-甚至其他人发表的一些无锁结构也被破坏了.

If you are asking this question, you do not probably know enough to be able to create a lock free structure. Creating lock free structures is extremely hard, and only experts in this field can do it. Instead of writing your own, search for an existing implementation. When you find it, check how widely it is used, how well is it documented, if it is well proven, what are the limitations - even some lock free structure other people published are broken.

如果找不到与当前使用的结构相对应的无锁结构,请改编算法,以便可以使用一些现有的无锁结构.

If you do not find a lock free structure corresponding to the structure you are currently using, rather adapt the algorithm so that you can use some existing one.

如果您仍然坚持创建自己的无锁结构,请确保:

If you still insist on creating your own lock free structure, be sure to:

  • 从非常简单的事情开始
  • 了解目标平台的内存模型(包括读/写重排序约束,哪些操作是原子操作)
  • 研究很多其他人在实现无锁结构时遇到的问题
  • 不要只是猜测它是否可以工作,请证明
  • 大量测试结果

更多阅读内容:

在Wikipedia上锁定免费并等待免费的算法

草药销售商:无锁代码:一种错误的安全感

更多推荐

如何编写无锁结构?

本文发布于:2023-11-30 20:02:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651327.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:结构

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!