多生产者多消费者无锁(甚至无等待)队列
我正在寻找有关如何将 MP/MC 队列编写为无锁甚至无等待的文档。我正在使用.Net 4.0。发现了很多C++代码,但我对内存模型不是很熟悉,所以移植到C#时很有可能会引入一些bug。
I'm searching for documentation on how to write MP/MC queue to be lock-free or even wait-free. I'm using .Net 4.0. Found a lot of C++ code, but I'm not very familiar with memory models, so there is a big chance I will introduce some bugs while porting to C#.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
作为一个可供考虑的选项,有一种有界算法多生产者多消费者队列,作者:Dmitry Vyukov。我已将该算法移植到 .NET,您可以在 github 上找到源代码。速度非常快。
入队算法:
出队算法:
As an option to consider, there's an algorithm of the bounded Multiple Producer Multiple Consumer queue by Dmitry Vyukov. I've ported the algorithm to .NET, you can find the sources on github. It's very fast.
The enqueue algorithm:
The dequeue algorithm:
为什么你认为你需要无锁队列?您是否尝试过使用
ConcurrentQueue
,可能包含在BlockingCollection
?编写多线程代码很困难。编写无锁代码更加困难,除非确实必要,否则您不应该自己编写。
Why do you think you need lock-free queue? Have you tried using
ConcurrentQueue<T>
, possibly enclosed within aBlockingCollection<T>
?Writing multi-threaded code is hard. Writing lock-free code is even harder and you shouldn't do it yourself unless you really have to.
我的第一个选择是使用 ConcurrentQueue,但您可以将数据存储抽象到接口后面,以便可以轻松更改实现。然后对典型场景进行基准测试,看看在哪里遇到问题。请记住:过早的优化是万恶之源。设计您的系统,使其不依赖于实现,而是依赖于合同,然后您可以根据需要优化您的实现。
我用 ILSpy 查看了
ConcurrentQueue
,乍一看似乎是一个无锁实现 - 很有可能它正是您正在寻找的东西。My first go would be with
ConcurrentQueue<T>
but you can abstract your data store away behind an interface so you can easily change implementations. Then benchmark typical scenarios and see where you run into problems. Remember: Premature optimzation is the root of all evil. Design your system so it's not tied to an implementation but to a contract and then you can optimize your implementations all you want.I had a look at
ConcurrentQueue<T>
with ILSpy and seems to be a lock free implementation at first glance - so good chance it's exactly what your are looking for.