具有插入优先级的非优先级队列

发布于 2024-12-20 05:16:12 字数 487 浏览 2 评论 0原文

谷歌这个搜索词让我头疼。

我需要一个线程安全机制来实现以下目标。 插入优先级高于读取的线程安全列表。

我需要始终能够将消息(比如说)插入队列(或其他),并且偶尔能够读取。 因此,读取永远不会干扰插入。

谢谢。

编辑:阅读也意味着清除红色部分。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别理我 2024-12-27 05:16:12

将队列构造为对象的链接列表。保留对队列头和尾部的引用。请参阅下面的伪代码,它大致说明了这个想法

QueueEntity Head;
QueueEntity Tail

class QueueEntity
{
       QueueEntity Prev;
       QueueEntity Next;
       ...   //queue content; 
}

and then do this:

//Read
lock(Tail)
{
  //get the content
  Tail=Tail.Prev;
}

//Write
lock(Head)
{
   newEntity = new QueueEntity();
   newEntity.Next = Head ;
   Head.Prev = newEntity;
   Head = newEntity;
}

在这里,您有单独的用于读取和写入的锁,并且它们不会相互阻塞,除非您的队列中只有一个条目。

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

QueueEntity Head;
QueueEntity Tail

class QueueEntity
{
       QueueEntity Prev;
       QueueEntity Next;
       ...   //queue content; 
}

and then do this:

//Read
lock(Tail)
{
  //get the content
  Tail=Tail.Prev;
}

//Write
lock(Head)
{
   newEntity = new QueueEntity();
   newEntity.Next = Head ;
   Head.Prev = newEntity;
   Head = newEntity;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文