具有插入优先级的非优先级队列
谷歌这个搜索词让我头疼。
我需要一个线程安全机制来实现以下目标。 插入优先级高于读取的线程安全列表。
我需要始终能够将消息(比如说)插入队列(或其他),并且偶尔能够读取。 因此,读取永远不会干扰插入。
谢谢。
编辑:阅读也意味着清除红色部分。
EDIT2:也许有帮助,有一个读者和一个作者。
EDIT3:案例场景:1 分钟内每秒 10 次插入(或使用软件所在的硬件的最大可能)。然后插入暂停 1 分钟。然后在 30 秒的时间内在 2 秒内进行 20 次插入(或使用打开软件的硬件可能的最大插入次数)。然后暂停30秒。然后暂停用于最大读取次数。我不知道我说得够清楚吗?显然不是。 (PS:我不知道什么时候会发生暂停,这就是问题所在)。最大加速度插入延迟:Enqueue 或 Add 方法完成的时间。
另外:可以使用带有 AddOrUpdate 和 TryGetValue 和 TryRemove 的 ConcurrentDictionary 吗?
Google is giving me headaches with this search term.
I need a thread safe mechanism to achieve the following.
A thread safe list with insert priority over read.
I need to always be able to insert a message (let's say) to the queue (or whatever) and occasionally, be able to read.
So reading, cannot, ever, interfere with inserting.
Thanks.
EDIT: Reading would also mean clearing the red part.
EDIT2: Maybe helpful, there is a single reader and a single writer.
EDIT3: Case scenario: 10 inserts per second for a period of 1 minute (or max possible using the hardware on which the software is on). Then a insert pause of 1 minute. Then 20 inserts (or max possible using the hardware on which the software is on) in 2 seconds for a period of 30 sec. Then a pause of 30 sec. Then the pause is used for max number of reads. I don't know if I am being clear enough. Obviously not. (PS: I don't know when the pause will occur, that is the problem). Max acc. delay for insert: the time for the Enqueue or Add method to finish.
ADDITIONAL: Could a ConcurrentDictionary with a AddOrUpdate with TryGetValue and TryRemove be used?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将队列构造为对象的链接列表。保留对队列头和尾部的引用。请参阅下面的伪代码,它大致说明了这个想法
在这里,您有单独的用于读取和写入的锁,并且它们不会相互阻塞,除非您的队列中只有一个条目。
Construct your queue as a linked list of objects. Keep a reference to the head and the tail of the queue. See below the pseudo code which roughly tells the idea
Here you have separate locks for reading and writing and they won't block each other unless there is only one entiry in your queue.