public void Enumerate() { var queue = new SlimConcurrentQueue <int>(Enumerable.Range(0, 100)); int cur = 0; foreach (int i in queue) { Assert.AreEqual(cur++, i); } Assert.AreEqual(100, queue.Count()); cur = 0; foreach (int i in queue.DequeueAll()) { Assert.AreEqual(cur++, i); Assert.AreEqual(100 - cur, queue.Count()); } Assert.AreEqual(0, queue.Count()); queue.EnqueueRange(Enumerable.Range(0, 100)); Assert.AreEqual(100, queue.Count()); cur = 0; foreach (int i in queue.AtomicDequeueAll()) { Assert.AreEqual(cur++, i); Assert.AreEqual(0, queue.Count()); } }
public void ResetDeEnum() { var queue = new SlimConcurrentQueue <int>(Enumerable.Range(0, 100)); var en = queue.DequeueAll().GetEnumerator(); while (en.MoveNext() && en.Current < 50) { ; } en.Reset(); int cmp = 51; while (en.MoveNext()) { Assert.AreEqual(cmp++, en.Current); } }
public void HalfDequeue() { int cores = CoreCount(); if (cores < 2) { cores = 2; } var threads = new Thread[cores]; var queue = new SlimConcurrentQueue <int>(); var secQueue = new TrackedConcurrentQueue <int>(); for (int i = 0; i != cores; ++i) { if (i % 2 == 0) { threads[i] = new Thread(obj => { int off = (int)obj; for (int x = 0; x != 100000; ++x) { queue.Enqueue(x * cores + off); } }); } else { threads[i] = new Thread(obj => { foreach (int taken in queue.DequeueAll()) { secQueue.Enqueue(taken); } }); } } for (int i = 0; i < cores; i += 2) { threads[i].Start(i); } for (int i = 1; i < cores; i += 2) { threads[i].Start(i); } for (int i = 0; i != cores; ++i) { threads[i].Join(); } Dictionary <int, int> dict = new Dictionary <int, int>(); for (int i = 0; i != cores; ++i) { dict[i] = -1; } secQueue.EnqueueRange(queue); foreach (int test in secQueue) { int bucket = test % cores; int last = dict[bucket]; Assert.IsTrue(test > last); dict[bucket] = last; } }