c# – Thread safe queue – Enqueue / Dequeue

c# – Thread safe queue – Enqueue / Dequeue

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.

Any reason you cant use ConcurrentQueue<T>

c# – Thread safe queue – Enqueue / Dequeue

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

Leave a Reply

Your email address will not be published.