Reputation: 907
There is a ConcurrentQueue instance and two threads that access it concurrently. One thread enqueues items continuesly (may be enqueue few items in 100 micro seconds) and other thread TryDequeue item by item and do some processing. There is a ManualResetEvent to signal the processing thread after any item enqueue (this is not quite related to this question)
In this case, is there any possibility of adding items in wrong order to the ConcurrentQueue. I know it's thread safe but just want to make sure whether it does not mess with the order of items when enqueue and dequeue quite faster.
Upvotes: 1
Views: 4372
Reputation: 24857
It's a queue! If it could reorder entries, they would have called it ConcurrentBag or something. The only way things get out-of-order on producer-consumer queue pipelines is if there is more than one consumer thread.
Upvotes: 2
Reputation: 62439
There is absolutely no way that the items can be out of order.
First of all, a queue by definition enforces a FIFO order: you put elements at one end and take them out at the other end. And since this is done by a single-producer-single-consumer model, it's absolutely safe.
Upvotes: 9