检查队列< T>一直 [英] Checking a Queue<T> Continuously

查看:178
本文介绍了检查队列< T>一直的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个函数来检查队列为新增加不断上一个线程

I would like a function to check a Queue for new additions continuously on one thread

显然,是一个连续的循环与休眠的选项,但我想要的东西减少浪费。

Obviously there is the option of a continuous loop with sleeps, but I want something less wasteful.

我认为是某种类型的等待句柄,然后有队列信号,但我不能覆盖排队安全,因为它不是虚拟的。

I considered a wait handle of some type and then having the queue signal it, but I can't override Enqueue safely as it is not virtual.

现在我正在考虑封装问答LT; T>!作为我最好的选择,但我想问你罚款乡亲,如果有一个更好的

Now I'm considering encapsulating a Queue<T> as my best option but I wanted to ask you fine folks if there were a better one!

我们的想法是:我想许多线程访问一个套接字连接,同时保证他们阅读只为他们的消息的反应,所以我将有一个线程调度和读取回应,然后执行与响应数据的回调(以纯文本)

The idea is: I want many threads to access a socket connection while guaranteeing they read only the response for their message, so I was going to have one thread dispatch and read responses and then execute a callback with the response data (in plain text)

推荐答案

尝试阻塞队列:的创建阻塞队列&LT; T&GT;在.NET?

其基本思想是,当你调用 TryDequeue 将阻塞,直到有什么东西在队列中。正如你所看到的阻塞队列的美是,你不必轮询/休眠或做任何疯狂的这样的...这是一个生产者/消费者模式的基本支柱。

The basic idea is that when you call TryDequeue it will block until there is something in the queue. As you can see "beauty" of the blocking queue is that you don't have to poll/sleep or do anything crazy like that... it's the fundamental backbone for a Producer/Consumer pattern.

我的阻塞队列的版本是:

My version of the blocking queue is:

public class BlockingQueue<T> where T : class
{
    private bool closing;
    private readonly Queue<T> queue = new Queue<T>();

    public int Count
    {
        get
        {
            lock (queue)
            {
                return queue.Count;
            }
        }
    }

    public BlockingQueue()
    {
        lock (queue)
        {
            closing = false;
            Monitor.PulseAll(queue);
        }
    }

    public bool Enqueue(T item)
    {
        lock (queue)
        {
            if (closing || null == item)
            {
                return false;
            }

            queue.Enqueue(item);

            if (queue.Count == 1)
            {
                // wake up any blocked dequeue
                Monitor.PulseAll(queue);
            }

            return true;
        }
    }


    public void Close()
    {
        lock (queue)
        {
            if (!closing)
            {
                closing = true;
                queue.Clear();
                Monitor.PulseAll(queue);
            }
        }
    }


    public bool TryDequeue(out T value, int timeout = Timeout.Infinite)
    {
        lock (queue)
        {
            while (queue.Count == 0)
            {
                if (closing || (timeout < Timeout.Infinite) || !Monitor.Wait(queue, timeout))
                {
                    value = default(T);
                    return false;
                }
            }

            value = queue.Dequeue();
            return true;
        }
    }

    public void Clear()
    {
        lock (queue)
        {
            queue.Clear();
            Monitor.Pulse(queue);
        }
    }
}

非常感谢马克Gravell 的这一个!

这篇关于检查队列&LT; T&GT;一直的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆