Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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 { } 
}

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
Posted
Updated 28-Mar-11 10:37am
v2
Comments
Wendelius 28-Mar-11 16:37pm    
Readability modifications
Steve Wellens 28-Mar-11 17:14pm    
catch { }

Really? That's the equivalent of disconnecting the smoke alarms in your children's bedrooms or putting black tape over the idiot lights in your car. Not good. Bad. Really bad.
#realJSOP 28-Mar-11 17:24pm    
He probably just did that for the sake of brevity...
Steve Wellens 28-Mar-11 17:35pm    
I hope that's true. Nevertheless, I'll be up all night worrying about it.

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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-Mar-11 20:02pm    
Kythen, this is a good answer, my 5.
There is only one thing you probably don't know yet: the analog of ConcurrentQueue for .NET 2.0-3.5 does exist. It is in my article, fully implemented.
Please see my Answer and the referenced article.
--SA
Kythen 28-Mar-11 20:31pm    
Thanks for the tip! :)
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900