I currently have an app that is receiving packets from a socket, processing them and adding them to a ConcurrentQueue. I then have a separate thread that is processing these items.
The problem I am having is the Producer/Consumer issue where the consumer is trying to take items even when there are not any items, resulting in significant cpu usage.
ProcessPackets is run on its own thread:
private ConcurrentQueue<PrimaryPacket> Waiting = new ConcurrentQueue<PrimaryPacket>();
private void ProcessPackets()
{
PrimaryPacket e;
while (true)
{
if (Waiting.TryDequeue(out e))
{
Packets.TryAdd(((ulong)e.IPAddress << 32 | e.RequestID), e);
}
}
}
public void AddPacket(PrimaryPacket e)
{
Waiting.Enqueue(e);
}
What would be the best way of implementing the BlockingCollection(T) to deal with this issue? Or another solution?
Also noteworthy, there is about 30,000 items being added to the queue per second.