如何编写无锁结构? [英] How can I write a lock free structure?

查看:83
本文介绍了如何编写无锁结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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上锁定免费并等待免费的算法

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

这篇关于如何编写无锁结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆