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
(And for Queue, they say it is not thread-safe). Looking at the internals is a weak test: The internals might change any time to a non-thread-safe version. Don't rely on undocumented properties except under unusual circumstances.
Queue class also provides FIFO data structure but it is not safe to use with multi-threading environment. To provide thread-safety, we have to implement locking around Queue methods which is always error prone.
Enqueue adds an element to the end of the Queue. Dequeue removes the oldest element from the start of the Queue. Peek returns the oldest element that is at the start of the Queue but does not remove it from the Queue. The capacity of a Queue is the number of elements the Queue can hold.
Concurrent Queue. A concurrent queue is basically a queue which provides protection against multiple threads mutating its state and thus causing inconsistencies. A naive way to implement a concurrent queue may be to just slap locks in its enqueue and dequeue functions when they try to modify the head and tail.
Any reason you can't use ConcurrentQueue<T>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With