NextQueueGroup.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace PlatformBenchmarks
  5. {
  6. public class NextQueueGroup
  7. {
  8. private List<NextQueue> mQueues = new List<NextQueue>();
  9. public NextQueueGroup(int count = 0)
  10. {
  11. if (count == 0)
  12. {
  13. count = Math.Min(Environment.ProcessorCount, 16);
  14. }
  15. for (int i = 0; i < count; i++)
  16. mQueues.Add(new NextQueue());
  17. }
  18. private long mIndex = 0;
  19. public void Enqueue(IEventWork item, int waitLength = 5)
  20. {
  21. for (int i = 0; i < mQueues.Count; i++)
  22. {
  23. if (mQueues[i].Count < waitLength)
  24. {
  25. mQueues[i].Enqueue(item);
  26. return;
  27. }
  28. }
  29. Next().Enqueue(item);
  30. }
  31. public NextQueue Next(int waitLength)
  32. {
  33. for (int i = 0; i < mQueues.Count; i++)
  34. {
  35. if (mQueues[i].Count < waitLength)
  36. {
  37. return mQueues[i];
  38. }
  39. }
  40. return Next();
  41. }
  42. public NextQueue Next()
  43. {
  44. var index = System.Threading.Interlocked.Increment(ref mIndex);
  45. return mQueues[(int)(index % mQueues.Count)];
  46. }
  47. }
  48. }