互斥问题 [英] mutual exclusion issue

查看:218
本文介绍了互斥问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我有一个使用集合的应用程序,在这种情况下,该集合使用来自不同线程的队列.一个对象在一个线程中入队,而另一个在另一个线程中出队.这些操作可能同时累积,这将在重新定义集合计数器时解决异常,例如参数超出范围异常.

现在,我正在寻找一种好看"的正确方法,以将这些动作相互排斥

(1)好看"的意思是我不想创建自己的集合,而这个集合包括lock(object)机制

(2)我不想使用我的脑力激荡的想法,这很丑陋"

Hello

I''ve got an application which uses a collection in this case a queue from diffrent threads. An object gets enqueued in one thread while another gets dequeued in a different thread. These actions might accure simultaneously which would resolve in an exception such as argument out of range exception , when the collections counter is being redefined.

Now im looking for a "good looking" and right way to exclude these actions from one another

(1) What I mean by "good looking" is that i don''t want to create my own collection derived from this one wich includes a lock(object) mechanism

(2) I don''t want to use the brain storming idea i had, which is pretty "ugly"

enqueueOk = false;
while (enqueueOk)
{
       try
       {
           Qsockets.Enqueue(currentSoc);
           reEnqueueOk = true;
       }
       catch { } 
}


我想当然会使用锁或互斥,但是只有当我将这些动作包装在从每个线程调用的过程中,并决定要入队还是出队时(也将是漫长而丑陋的"),情况才会如此

任何想法都会被应用...
10x


I thought of course using the a lock or a mutex but that would be the case only if I wrap these actions in a procedure which would be called from each thread, and decide either to enqueue or dequeue which would also be long and "ugly"

Any ideas would be appricated ...
10x

推荐答案

如果您使用的是.NET 4,则可以使用 System.Collections.Queue(T) [系统.Collections.Generic.SynchronizedCollection(T) [ ^ ]实例.
If you''re using .NET 4 you can use the System.Collections.Concurrent.ConcurrentQueue(T)[^] class.

If you''re using .NET 3.5 or older, you could maybe create a derived class from System.Collections.Queue(T)[^] and employ a System.Collections.Generic.SynchronizedCollection(T)[^] instance internally.


如果互斥是不正确的(它不能保证互斥),并且您不接受该行为,那么您会天真地实现"性能原因:它将毫无理由地占用其中一个核心的CPU时间的100%.这称为自旋等待,永远不要使用.所有等待都应在线程同步原语(例如EventWaitHandle)上完成.在这种情况下,线程不运行任何代码并且消耗零CPU时间:它由OS切换,从不调度,直到被同步原语唤醒.

如果除了Kythen的答案之外:如果在4.0之前(实际上是2.0到3.5)需要.NET Framework的类似队列,则可以使用与ConcurrentQueue完全相似的通用类.

在这里查看我的提示/技巧文章:用于线程通信的简单阻塞队列和线程间调用 [ ^ ] —我提供了完整的源代码和用法示例.

—SA
You naive "implementation" if mutual exclusion is incorrect (it does not guarantee mutual exclusion) and cannot be accepted for performance reason: it will occupy 100% of CPU time of one of the cores without a reason. It is called spin-wait which never should be used. All waits should be done on thread synchronization primitives (such as EventWaitHandle. In this case, the thread do not run any code and consume zero CPU time: it is switched of by OS and never scheduled again until waken up by the synchronization primitive.

If addition to the Answer by Kythen: if you need the similar queue for .NET framework prior to 4.0 (actually 2.0 to 3.5), you can use my generic class fully analogous to ConcurrentQueue.

See my Tips/Tricks article here: Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^] — I provide complete source code with usage samples.

—SA


这篇关于互斥问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆