Luk*_* Vo 5 .net c# multithreading priority-queue .net-6.0
.NET 6 现在有PriorityQueue<TElement,TPriority>这非常有用。该文档还不是很清楚(在提出问题时,该文档仍然适用于 RC1)是否是线程安全的。两件事情:
它驻留在 中System.Collections.Generic,并且 中似乎没有等效项System.Collections.Concurrent。
TryDequeue它确实有名为和 的方法TryPeek。当然,它们可能只是在队列为空时不会抛出异常的方法,但它确实给人一种并发集合的印象。
我可以将它用于多线程环境而无需包装/锁定(例如在 ASP.NET Core 网站中)吗?我不知道的任何并发等效项(如果可能的话,我尝试不使用第 3 方包)?
spe*_*der 13
例如,查看 的源代码PriorityQueue.Enqueue,您会立即发现该代码不是线程安全的:
public void Enqueue(TElement element, TPriority priority)
{
// Virtually add the node at the end of the underlying array.
// Note that the node being enqueued does not need to be physically placed
// there at this point, as such an assignment would be redundant.
int currentSize = _size++; // <-- BOOM
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2335 次 |
| 最近记录: |