7

I often have the need to process a queue of items where no one user should be able to block the queue and the items in the queue should be processed in some order. I frequently write a class to do this, but I thought there should be some generic version but I can't find one.

So I'm looking for a queue class where I can specify a type, a selector for partitions and a selector to order by such that I can add objects to the queue and then when I get objects back out, I get the first object from the next partition ordered by my order specifier.

For example, I would call like this specifying how to partition and how to sort the queue:

 var queue = new RoundRobinQueue<Message>(
            _ => _.UserID,
            _ => _.SendDate
            );

And after I've added lots of Message's, I can Parallel.ForEach the items in my queue and process them in the order of the earliest SendDate for the next User. That way, if one user is slow, his items won't block the queue since he only gets one thread, but if there is only one user, he's the only partition so he gets all the threads.

I looked all over but couldn't find a nice generic implementation in C# for this. Any ideas?

Ian R. O'Brien
  • 6,682
  • 9
  • 45
  • 73
powlette
  • 1,800
  • 20
  • 41
  • foreach needs IEnumerable. But how could foreach know when to end if this collection, a round robin queue, always has next? You may force to break it by some count or timeout. But the users of this class can be hurt if they use it wrong, even yourself. E.g. this queue may be passed around in your code as IEnumerable, and passed to one method that take any IEnumerable and do foreach on it, Oops! – Ryan Apr 19 '13 at 00:02
  • Why not an extension method to `IEnumerable` that returns an Iterator ? – PPC Jun 22 '15 at 19:56

1 Answers1

-1

Look at the classes defined in System.Collections.Concurrent. There is a generic ConcurrentQueue as well as more basic building blocks for producer-consumer patterns.

The available classes and interfaces are summarized on MSDN.

Morten Mertner
  • 9,414
  • 4
  • 39
  • 56