31 namespace System.Threading.Tasks
34 internal class SimpleConcurrentBag<T>
36 readonly IConcurrentDeque<T>[] deques;
43 public SimpleConcurrentBag (
int num)
45 deques =
new CyclicDeque<T>[num];
46 for (
int i = 0; i < deques.Length; i++) {
47 deques[i] =
new CyclicDeque<T> ();
52 public int GetNextIndex ()
54 return Interlocked.Increment (ref index);
57 public bool TryTake (
int index, out T value)
61 return deques[index].PopBottom (out value) ==
PopResult.Succeed;
64 public bool TrySteal (
int index, out T value)
71 const int roundThreshold = 3;
73 for (
int round = 0; round < roundThreshold; ++round) {
74 if (stealIndex == index)
75 stealIndex = (stealIndex + 1) % deques.Length;
77 if (deques[(stealIndex = (stealIndex + 1) % deques.Length)].PopTop (out value) ==
PopResult.Succeed)
84 public void Add (
int index, T value)
86 deques[index].PushBottom (value);