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:
Thanks
I would start with option 2. Use a regular queue and lock it.
If you already have a lock there should be little reason to use a thread safe collection. Just note that while the locking overhead is low for uncontested locks, if your list is accessed very frequently from multiple threads you might want some other strategy. But designing high performance thread safe collections is difficult, and not something I would attempt without a great deal of study.
As always, profile your algorithm & program to find out the bottlenecks and usage patterns.