问题描述
你好
我有一个使用集合的应用程序,在这种情况下,该集合使用来自不同线程的队列.一个对象在一个线程中入队,而另一个在另一个线程中出队.这些操作可能同时累积,这将在重新定义集合计数器时解决异常,例如参数超出范围异常.
现在,我正在寻找一种好看"的正确方法,以将这些动作相互排斥
(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
推荐答案
这篇关于互斥问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!