33

Firstly, i'll explain a short scenario;

As a signal from certain devices triggers, an object of type Alarm is added to a queue. At an interval, the queue is checked, and for each Alarm in the queue, it fires a method.

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

Kestami
  • 2,045
  • 3
  • 32
  • 47
  • You should pop items from the queue first and SendAlarm second, as well as using a generic thread-safe queue implementation. If you can't handle an item then enqueue it again. – cdleonard Nov 16 '12 at 13:07

5 Answers5

35
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:

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. 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. Using the TryQueue approach, you are guaranteed that each Alarm in the queue would only get processed once; 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.

If you want to process each alarm exactly once, and subsequently remove it from the queue, you should use the first approach.

Douglas
  • 53,759
  • 13
  • 140
  • 188
  • So just for reference, the TryDequeue pushes out an alarm, which we populate into out previously declared alarm variable, and then use it? – Kestami Nov 16 '12 at 13:11
  • Exactly. Also, `TryDequeue` returns `false` when the list is empty, causing us to break out of the `while` loop. – Douglas Nov 16 '12 at 13:13
  • that's pretty nifty : ) going to test this now. – Kestami Nov 16 '12 at 13:14
  • First approach worked a charm, exactly what i wanted. Guess it's one of those things, once you know it you know it! Thanks – Kestami Nov 16 '12 at 13:21
32

Any reason you can't use ConcurrentQueue<T>

hometoast
  • 11,522
  • 5
  • 41
  • 58
10

.Net already has a thread-safe queue implementation: have a look at ConcurrentQueue.

jeroenh
  • 26,362
  • 10
  • 73
  • 104
  • i'm having some trouble with ".TryDequeue()". Could you perhaps help me out? : ) i'll update my question to a ConcurrentQueue – Kestami Nov 16 '12 at 13:05
0

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.

Thought
  • 700
  • 2
  • 9
  • 21
-1

If you want to use the queue for different threads, you must lock the queue. Like the example below:

public object LockQ = new object()
public Queue<Alarm> alarmQueue = new Queue<Alarm>();

thread1: Enqueue()

while (true)
{
    lock (LockQ)
    {
        alarmQueue.Enqueue(alarm);
    }
}

thread2: Dequeue()

while (alarmQueue.Count > 0)
{
    lock (LockQ)
    {
        var _Alarm = alarmQueue.Dequeue();
    }
}

But the optimal solution to use the queue in different threads is to use ConcurrentQueue

public ConcurrentQueue<Alarm> alarmQueue = new ConcurrentQueue<Alarm>();

thread1: Enqueue()

while (true)
{
    alarmQueue.Enqueue(alarm);
}

thread2: Dequeue()

while (alarmQueue.TryDequeue(out _Alarm))
{
    SendAlarm(_Alarm);
}
Hossein Sabziani
  • 1
  • 2
  • 15
  • 20