I have a list of items and I would like to retreive items from this list according to an algorithm. I will use strategy pattern to inject algorithms like FIFO, MFU, LRU, etc. For example, in FIFO I planned to use a concurrent queue to peek the first element and then move it to the end. The problem is that Enqueue and Dequeue are tread safe, but not atomic. This means that two threads may access in parallel when I call to the two methods (the first thread may finish dequeue and the second will dequeue before enqueue on first thread finished).
I was asking myself what is the better approach to solve this:
- Add lock around the two methods? (Dequeue and Enqueue)
- Lock two methods but using a regular Queue to avoid double lock (my lock and the concurrent queue lock)
- Use any other thread safe collection?
Thanks