简体   繁体   English

C#中的无锁优先队列

[英]A lock-free priority queue in C#

I have been searching lately for information on how to construct a lock-free priority queue in C#.我最近一直在寻找有关如何在 C# 中构建无锁优先级队列的信息。 I have yet to even find an implementation in any language, or a decent paper on the matter.我什至还没有找到任何语言的实现,或者关于这个问题的像样的论文。 I have found several papers which appear to be copies or at least referencing one particular paper which is not actually a paper on lock free priority queues, despite its name;我发现几篇论文似乎是副本,或者至少引用了一篇特定的论文,尽管它的名字实际上并不是关于无锁优先队列的论文; it is in fact a paper on a priority queue which uses fine grained locks.它实际上是一篇关于使用细粒度锁的优先级队列的论文。

The responses I have been receiving from elsewhere include "use a single thread" and "you do not need it to be lock free" and "it is impossible".我从其他地方收到的答复包括“使用单个线程”和“您不需要它是无锁的”和“这是不可能的”。 All three of these responses are incorrect.这三个回答都是错误的。

If someone has some information on this, I would greatly appreciate it.如果有人有这方面的信息,我将不胜感激。

Generally, it's a bad idea to write this kind of code yourself .一般来说, 自己编写这种代码是一个坏主意

However, if you really want to write this kind of code, I say take a page from Eric Lippert's book (or blog, as it were) (web archive link) , where basically, you would implement the queue but instead of having all the functions that make modifications on the queue modify the instance you call the method on, the methods return completely new instances of the queue.但是,如果您真的想编写这种代码,我说从Eric Lippert 的书(或博客,实际上) (网络存档链接)中获取一页,基本上,您将实现队列,而不是拥有所有对队列进行修改的函数会修改您调用该方法的实例,这些方法返回队列的全新实例

This is semantically similar to the pattern that System.String uses to maintain immutability;这在语义上类似于System.String用于维护不变性的模式; all operations return a new System.String , the original is not modified.所有操作都返回一个新的System.String ,原始未修改。

The result of this is that you are forced to reassign the reference returned on every call.这样做的结果是您被迫重新分配每次调用时返回的引用。 Because the assignments of references are atomic operations, there is no concern about thread-safety;因为引用的赋值是原子操作,所以不用担心线程安全; you are guaranteed that the reads/writes will be atomic.你保证读/写将是原子的。

However, this will result in a last-in-wins situation;然而,这将导致最后胜利的局面; it's possible that multiple modifications are being made to the queue, but only the last assignment will hold, losing the other insertions into the queue.可能对队列进行了多次修改,但只有最后一个分配会保留,而丢失了其他插入队列的内容。

This might be acceptable;这可能是可以接受的; if not, you have to use synchronization around the assignment and reading of the reference.如果没有,您必须在引用的分配和读取周围使用同步。 You will still have a lock-free-priority queue, but if you have concerns about thread-safety and maintaining the integrity of the operations, you have done nothing but move the concern about synchronization outside of the data structure (which is almost all cases, is a good thing, as it gives you fine-grained explicit control).您仍然会有一个无锁优先级队列,但是如果您担心线程安全和维护操作的完整性,那么您除了将同步的关注移到数据结构之外(几乎所有情况下) , 是一件好事,因为它为您提供了细粒度的显式控制)。

The Art of Multiprocessor Programming .多处理器编程的艺术 Look at Chapter 15 - Priority Queues.请参阅第 15 章 - 优先级队列。 Book is in Java, but can be easily translated to C# since they both have GC (which is important for most implementations in the book).本书使用 Java 编写,但可以轻松转换为 C#,因为它们都具有 GC(这对于本书中的大多数实现很重要)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM