我有一个场景,我有多个线程添加到队列和多个线程从同一队列读取.如果队列达到特定大小,则填充队列的所有线程将在添加时被阻止,直到从队列中删除项目为止.
下面的解决方案就是我现在正在使用的问题,我的问题是:如何改进?是否有一个对象已经在我应该使用的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的无锁容器库?
最好的东西被证明比我们在.NET中使用的Synchronized包装器更有效.
我在.NET上发现了一些文章,但没有一篇文章指出任何速度基准测试,也没有激发他们对可靠性的信心.
谢谢
.net multithreading synchronization lock-free data-structures
在Windows窗体应用程序中,调用的影响Thread.Sleep(1)如下面的代码所示:
public Constructor()
{
Thread thread = new Thread(Task);
thread.IsBackground = true;
thread.Start();
}
private void Task()
{
while (true)
{
// do something
Thread.Sleep(1);
}
}
Run Code Online (Sandbox Code Playgroud)
这个线程会占用所有可用的CPU吗?
我可以使用哪些分析技术来测量此线程的CPU使用率(除了任务管理器)?
我正在寻找.NET的线程安全阻塞队列的实现.通过"线程安全阻塞队列"我的意思是: - 线程安全访问队列,其中Dequeue方法调用阻塞一个线程,直到其他线程放入(Enqueue)某个值.
到目前为止,我发现了这个:http://www.eggheadcafe.com/articles/20060414.asp (但它适用于.NET 1.1).
有人可以评论/批评这种实施的正确性.或建议另一个.提前致谢.