I'm trying to figure out a way to thread safely and performance wise undo a TryDequeue on a ConcurrentQueue
My current solution to the problem is:
lock(_queue) {
_queue = new ConcurrentQueue<IItem>(_queue.Reverse());
_queue.Enqueue(item);
_queue = new ConcurrentQueue<IItem>(_queue.Reverse());
}
But I don't know if this is the best way to do this, cause I'm locking it manually. But I need to be sure that the queue will not be altered in another thread during this process.
The reason for having to do this (I think) is that I do a TryPeek, do a check on the returned item, if it meets a criteria, I do a TryDequeue, and do the check again, if it fails the second time I want to do a rollback. Does this make sense?