相关疑难解决方法(0)

在.NET中创建阻塞队列<T>?

我有一个场景,我有多个线程添加到队列和多个线程从同一队列读取.如果队列达到特定大小,则填充队列的所有线程将在添加时被阻止,直到从队列中删除项目为止.

下面的解决方案就是我现在正在使用的问题,我的问题是:如何改进?是否有一个对象已经在我应该使用的BCL中启用此行为?

internal class BlockingCollection<T> : CollectionBase, IEnumerable
{
    //todo: might be worth changing this into a proper QUEUE

    private AutoResetEvent _FullEvent = new AutoResetEvent(false);

    internal T this[int i]
    {
        get { return (T) List[i]; }
    }

    private int _MaxSize;
    internal int MaxSize
    {
        get { return _MaxSize; }
        set
        {
            _MaxSize = value;
            checkSize();
        }
    }

    internal BlockingCollection(int maxSize)
    {
        MaxSize = maxSize;
    }

    internal void Add(T item)
    {
        Trace.WriteLine(string.Format("BlockingCollection add waiting: {0}", Thread.CurrentThread.ManagedThreadId));

        _FullEvent.WaitOne();

        List.Add(item);

        Trace.WriteLine(string.Format("BlockingCollection …
Run Code Online (Sandbox Code Playgroud)

.net c# queue collections multithreading

161
推荐指数
6
解决办法
10万
查看次数

互斥体真的慢了吗?

我已经阅读了很多次,无论是在网络上还是网络上,互斥体都比关键部分/信号量/插入你的首选同步方法慢.但我从未见过任何论文或研究或其他什么来支持这一主张.

那么,这个想法来自哪里?这是神话还是现实?互斥体真的慢吗?

windows multithreading mutex

19
推荐指数
3
解决办法
6724
查看次数

标签 统计

multithreading ×2

.net ×1

c# ×1

collections ×1

mutex ×1

queue ×1

windows ×1