简体   繁体   English

如何实现BlockingCollection来修复此生产者/消费者问题?

[英]How to implement BlockingCollection to fix this Producer/Consumer issue?

I currently have an app that is receiving packets from a socket, processing them and adding them to a ConcurrentQueue. 我目前有一个应用程序正在从套接字接收数据包,处理它们并将它们添加到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. 我遇到的问题是生产者/消费者问题,即使没有任何项目,消费者也试图获取物品,从而导致显着的CPU使用率。

ProcessPackets is run on its own thread: ProcessPackets在自己的线程上运行:

    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? 实现BlockingCollection(T)来处理这个问题的最佳方法是什么? Or another solution? 或另一种解决方案

Also noteworthy, there is about 30,000 items being added to the queue per second. 另外值得注意的是,每秒大约有30,000个项目被添加到队列中。

You don't have to implement BlockingCollection<T> , you can just use it. 您不必实现BlockingCollection<T> ,您可以使用它。 As the documentation says, it's just a wrapper for IProducerConsumerCollection<T> , such as ConcurrentQueue<T> (which is the default one). 正如文档所说,它只是IProducerConsumerCollection<T>的包装器,例如ConcurrentQueue<T> (这是默认值)。

private BlockingCollection<PrimaryPacket> Waiting =
    new BlockingCollection<PrimaryPacket>();

private void ProcessPackets()
{
    while (true)
    {
        PrimaryPacket e = Waiting.Take();
        Packets.TryAdd(((ulong)e.IPAddress << 32 | e.RequestID), e);
    }
}

public void AddPacket(PrimaryPacket e)
{
    Waiting.Add(e);
}

Take() blocks if the queue is empty, so it won't burn CPU unnecessarily. 如果队列为空则Take()阻塞,因此不会不必要地烧掉CPU。 And you should probably consider what to do when the processing is finished. 你应该考虑在处理完成后该怎么做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用BlockingCollection <>解决生产者/消费者竞争条件 - How to solve producer/consumer race condition with BlockingCollection<> 带有BlockingCollection的消费者/生产者看起来很慢 - Consumer/Producer with BlockingCollection appears slow 生产者/消费者,BlockingCollection和等待更改 - Producer/Consumer, BlockingCollection, and waiting for changes 如何使用 BlockingCollection 通知生产者-消费者模式中的失败? - How to notify failure in producer-consumer pattern using BlockingCollection? 在BlockingCollection生产者消费者中完成与IDisposable的比较 - Finalize vs. IDisposable in BlockingCollection Producer Consumer 生产者消费者有单独的类,具有共同的BlockingCollection - Producer Consumer Separate Classes with common BlockingCollection 无需BlockingCollection的简单生产者-消费者集合 - Simple producer-consumer collection without BlockingCollection C#BlockingCollection生成者使用者而不阻塞使用者线程 - C# BlockingCollection producer consumer without blocking consumer thread 如何实现这种特定的生产者-消费者模式 - How to implement this specific Producer-Consumer pattern 当生产者也是消费者时,如何在生产者/消费者模式中使用阻塞收集 - 我该如何结束? - How do I use a blockingcollection in the Producer/Consumer pattern when the producers are also the consumers - How do I end?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM