简体   繁体   English

线程安全队列 - 入队/出队

[英]Thread safe queue - Enqueue / Dequeue

Firstly, i'll explain a short scenario; 首先,我将解释一个简短的场景;

As a signal from certain devices triggers, an object of type Alarm is added to a queue. 当来自某些设备的信号触发时,会将类型为Alarm的对象添加到队列中。 At an interval, the queue is checked, and for each Alarm in the queue, it fires a method. 每隔一段时间检查一次队列,对于队列中的每个Alarm,它会触发一个方法。

However, the problem i'm running into is that, if an alarm is added to the queue whilst it's being traversed, it throws an error to say that the queue has changed whilst you were using it. 但是,我遇到的问题是,如果在遍历队列时将警报添加到队列中,则会在您使用它时发出错误说队列已更改。 Here's a bit of code to show my queue, just assume that alarms are being constantly inserted into it; 这里有一些代码来显示我的队列,只是假设警报不断插入其中;

public class AlarmQueueManager
{
    public ConcurrentQueue<Alarm> alarmQueue = new ConcurrentQueue<Alarm>();
    System.Timers.Timer timer;

    public AlarmQueueManager()
    {
        timer = new System.Timers.Timer(1000);
        timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
        timer.Enabled = true;
    }

    void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        DeQueueAlarm();
    }

    private void DeQueueAlarm()
    {
        try
        {
            foreach (Alarm alarm in alarmQueue)
            {
                SendAlarm(alarm);
                alarmQueue.TryDequeue();
                //having some trouble here with TryDequeue..

            }
        }
        catch
        {
        }
    }

So my question is, how do i make this more...thread safe? 所以我的问题是,我如何使这更多......线程安全? So that i won't run into these issues. 所以我不会遇到这些问题。 Perhaps something along the lines of, copying the queue to another queue, working on that one, then dequeueing the alarms that were dealt with from the original queue? 也许是这样的事情,将队列复制到另一个队列,处理那个队列,然后将从原始队列处理的警报出列?

edit: just been informed of concurrent queue, will check this out now 编辑:刚刚被告知并发队列,现在将检查出来

private void DeQueueAlarm()
{
    Alarm alarm;
    while (alarmQueue.TryDequeue(out alarm))
        SendAlarm(alarm);
}

Alternatively, you could use: 或者,您可以使用:

private void DeQueueAlarm()
{
    foreach (Alarm alarm in alarmQueue)
        SendAlarm(alarm);
}

Per the MSDN article on ConcurrentQueue<T>.GetEnumerator : 根据关于ConcurrentQueue<T>.GetEnumerator的MSDN文章:

The enumeration represents a moment-in-time snapshot of the contents of the queue. 枚举表示队列内容的时刻快照。 It does not reflect any updates to the collection after GetEnumerator was called. 调用GetEnumerator后,它不会反映对集合的任何更新。 The enumerator is safe to use concurrently with reads from and writes to the queue. 枚举器可以安全地与队列的读取和写入同时使用。

Thus, the difference between the two approaches arises when your DeQueueAlarm method is called concurrently by multiple threads. 因此,当多个线程同时调用DeQueueAlarm方法时,会出现两种方法之间的差异。 Using the TryQueue approach, you are guaranteed that each Alarm in the queue would only get processed once; 使用TryQueue方法,可以保证队列中的每个Alarm只会被处理一次; however, which thread picks which alarm is determined non-deterministically. 但是,哪个线程选择哪个警报是非确定性的。 The foreach approach ensures that each racing thread will process all alarms in the queue (as of the point in time when it started iterating over them), resulting in the same alarm being processed multiple times. foreach方法确保每个赛车线程将处理队列中的所有警报(从它开始迭代它们的时间点开始),导致多次处理相同的警报。

If you want to process each alarm exactly once, and subsequently remove it from the queue, you should use the first approach. 如果要仅准确处理每个警报一次,然后将其从队列中删除,则应使用第一种方法。

任何原因你不能使用ConcurrentQueue<T>

.Net已经有一个线程安全的队列实现:看看ConcurrentQueue

A better way to approach it, given that each thread is actually only processing a single Alarm at once, would be replacing this: 一个更好的方法来处理它,假设每个线程实际上只是一次处理一个警报,将取代这个:

        foreach (Alarm alarm in alarmQueue)
        {
            SendAlarm(alarm);
            alarmQueue.TryDequeue();
            //having some trouble here with TryDequeue..
        }

with this: 有了这个:

        while (!alarmQueue.IsEmpty)
        {
            Alarm alarm;
            if (!alarmQueue.TryDequeue(out alarm))  continue;
            SendAlarm(alarm);
        }

There's no reason to get a complete snapshot of the queue at any time at all, because you only truly care about the next one to process at the beginning of each cycle. 根本没有理由在任何时候获得队列的完整快照,因为您只关心在每个周期开始时要处理的下一个队列。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM