2

I need the following collection:

1 - Fixed-size length. So, it will automatically dequeue the tail when it reaches the fixed-size limit.

2 - Can access individual elements, not necessarily the head or tail only.

3 - FIFO.

4 - Allows safe concurrent access (however, I can compromise on this bit for now).

5 - Enqueue and Dequeue methods.

I am using .NET 4.5 and aware of the ConcurrentQueue class, however, it is missing points 1 and 2. I am thinking of building my class that implements IEnumerable and uses IList in the background.

I could inherit from ConcurrentQueue, but I need to continuously access body elements (not just the head and tail) and enumerating it every time would be inefficient.

Do you have a better approach or do you recommend any collection that does a similar job?

2
  • Are you need concurrent ring buffer? Dec 21, 2012 at 20:50
  • @HamletHakobyan Yes, but this is more like nice to have.
    – Adam
    Dec 21, 2012 at 21:05

2 Answers 2

0

Use queue implementation based on old plain System.Array. Your points:

1 - Array has fixed length.

2 - Can access individual elements in O(1) by definition.

3 - This will be queue, so it FIFO.

4 - Array will be concurrent safe, just use lock for Enqueue and Dequeue methods.

5 - Implement them easily.

4
  • Wouldn't a List<T> be a better option than the general System.Array? Are there any advantages of System.Array over List with respect to the question context?
    – Adam
    Dec 21, 2012 at 21:13
  • Really I do not see any big difference between those two with respect to your question. You can implement your generic queue datatype, based on List<T>. It will be good. With my answer I wanted to show you that even simplest data structures will work good for your requirements. You just need to implement Enqueue and Dequeue methods.
    – SergeyS
    Dec 21, 2012 at 21:19
  • According to MSDN msdn.microsoft.com/en-us/library/system.array.aspx "Only the system and compilers can derive explicitly from the Array class". It is not sealed but its constructor is internal. The List or IList is still the preferred option.
    – Adam
    Dec 21, 2012 at 22:11
  • you do not need to derive from Array, you can encapsulate it
    – SergeyS
    Dec 23, 2012 at 13:24
0

Build a wrapper around a List or an array. Provide the queue methods your want, keep track of the size, etc etc. Program your own concurrency requirements into this class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.