etienne_marais
Honorary Master
I have a foreach loop that calls MyQueue.Deque() within the loop, it runs in a thread.
A different thread (with completely different function) can add to the Queue, thus modifying the collection which c# does not like when iterating over foreach (and rightly so).
If there is a 'clash' then I get an exception. Rare, but happens from time to time.
Is lock(MyQueue) over the foreachloop as well as the MyQueue.Enqueue(...) adequate or do I have to consider Mutex or perhaps something else ?
My understanding is that lock(...) {...} has more to do with protecting critical sections in code than actually locking the relevant object from access by ANY other ? Or is this not the case. I know Mutex can be program-wide (or even computer-wide).
A different thread (with completely different function) can add to the Queue, thus modifying the collection which c# does not like when iterating over foreach (and rightly so).
If there is a 'clash' then I get an exception. Rare, but happens from time to time.
Is lock(MyQueue) over the foreachloop as well as the MyQueue.Enqueue(...) adequate or do I have to consider Mutex or perhaps something else ?
My understanding is that lock(...) {...} has more to do with protecting critical sections in code than actually locking the relevant object from access by ANY other ? Or is this not the case. I know Mutex can be program-wide (or even computer-wide).
Last edited: