Hey, I'm trying to implement a ConcurrentQueue in C# for an asynchronous server. Items are being queued as soon as a complete message is received. To dequeue messages, I'm making a small number of threads to do the work of dequeueing and servicing the requests. This is inadequate as each thread is using a while loop, which consumes rather a large amount of processor time, for obvious reasons.
Would anyone know of a method of dequeueing messages when required but not consuming so much processing time.
{
...
for (int i = 0; i < 3; i++)
{
Thread t = new Thread(new ThreadStart(startParsingMessages));
t.Start();
}
...
}
private void startParsingMessages()
{
QueueContainer dequeued = null;
Console.WriteLine("Trying");
while (true)
{
if (queue.TryDequeue(out dequeued))
{
Console.WriteLine("processing queue");
ProcessMessage(dequeued.socket, dequeued.message);
}
}
}
ConcurrentQueue is a thread-safe FIFO data structure. It's a specialized data structure and can be used in cases when we want to process data in a First In First Out manner.
C# ConcurrentQueue is a thread-safe collection class. It is introduced in .NET 4.0 with other concurrent collection classes. It provides a thread-safe First-In-First-Out (FIFO) data structure. You can read more about Queue here. ConcurrentQueue exists in System.Collections.Concurrent namespace.
Class ConcurrentLinkedQueue<E> An unbounded thread-safe queue based on linked nodes. This queue orders elements FIFO (first-in-first-out).
Instead of using ConcurrentQueue<T>
directly, have you tried wrapping it in a BlockingCollection<T>
? Then you can use TryTake(out T, TimeSpan)
etc. I believe that's the expected use: that the concurrent collections themselves would usually be there just so you can select how the blocking collection will work.
That doesn't have to be the only use for these collections of course, but particularly for ConcurrentQueue<T>
, the producer/consumer queue scenario is the most common one - at which point BlockingCollection<T>
is the way to make it easy to do the right thing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With