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);
}
}
}
Instead of using
ConcurrentQueue<T>directly, have you tried wrapping it in aBlockingCollection<T>? Then you can useTryTake(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 pointBlockingCollection<T>is the way to make it easy to do the right thing.