Delphi 的线程安全优先级队列?

发布于 2024-12-22 01:32:54 字数 447 浏览 5 评论 0原文

我正在寻找在 Delphi 中实现的优先级队列,它可以在多线程环境中很好地工作。

理想情况下是无锁的,或者是为多线程插入/删除而设计的,比单线程实现的锁定包装器(我已经有了)更好。

特殊之处在于,在正常操作中,当顶部(最高优先级项目)发生变化时,只会有添加、删除和通知,而最高优先级项目的“弹出”操作应该很少发生。

它将用于看门狗/超时线程监视任务,在其他线程中执行,这些任务预计在大多数情况下会正常终止,因此它们只会从队列中添加/删除。超时线程本质上是在等待下一个超时事件,因此当最优先的事件发生变化时需要通知。

这些任务由脚本处理,可以随时安全终止。

如果有比优先级队列更好的算法,它们也可能是很好的答案!

编辑:根据 Martin James 的评论,另一个特殊之处是不同的超时值相对较少,对于每个超时值,问题就变成了 FIFO 队列的问题。

I'm looking for a priority queue implemented in Delphi that would work well in a multi-threaded environment.

Ideally lock-free, or designed for multi-threaded inserts/deletes with something better than a locked wrapper around a single-threaded implementation (which I already have).

The specificity is that in normal operation, there would be only adds, deletes, and notifications when the top (highest priority item) changes, while "pop" operations of the highest priority item should be very infrequent.

It would be used for a watchdog/timeout thread monitoring tasks, being executed in other threads, those task are expected to terminate normally most of the time, so they would just be added/removed from the queue. The timeout thread would essentially be waiting on the next timeout event, hence the need for notifications when the top-priority event changes.

The tasks are handled by scripts, which can be safely terminated at any time.

If there are better algorithms for this than a priority queue, they could be good answers too!

Edit: following a remark by Martin James, another specificity is that there are relatively few different timeout values, and for each timeout value, the problem becomes that of a FIFO queue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

送你一个梦 2024-12-29 01:32:54

Julian Bucknall(《Tomes of Delphi:算法与数据结构》作者)最近宣布发布 Delphi XE 版本EZDSL(Delphi 结构库)在他的博客中。

不幸的是,TThreadsafePriorityQueue(在 EZDSLPQu.PAS 中实现)是基于锁的。

我忍不住要分享这个好消息,我的另一个目的是呼吁他为回答这个问题做出贡献。

Julian Bucknall (Author of "Tomes of Delphi: Algorithms and Data Structures" ) has recently announced the release of a Delphi XE version of EZDSL(a Delphi Structures Library) in his Blog.

Unfortunately TThreadsafePriorityQueue (implemented in EZDSLPQu.PAS) is lock based.

I can't help sharing the good news and my other intent is a call for his contribution in answering the question.

妳是的陽光 2024-12-29 01:32:54

我的框架架构完全围绕优先级线程队列构建 - 这是我使用的唯一线程模型(http://www. csinnovations.com/framework_overview.htm)。学习曲线陡峭,但它可能会给你一些想法。

My framework architecture is completely built around priority threaded queues - that's the only threading model I use (http://www.csinnovations.com/framework_overview.htm). A steep learning curve, but it might give you some ideas.

一人独醉 2024-12-29 01:32:54

您可以为每个优先级使用一个队列,而不是单个队列。

OmnithreadLibrary 包含线程安全队列:
http://code.google.com/p/omnithreadlibrary/

http://code.google.com/p/omnithreadlibrary/

Instead of a single que you could use one queue for each priority.

The OmnithreadLibrary contains thread safe queues:
http://code.google.com/p/omnithreadlibrary/

http://code.google.com/p/omnithreadlibrary/

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