-
Notifications
You must be signed in to change notification settings - Fork 16
BlockingQueue
ikopylov edited this page Apr 12, 2017
·
6 revisions
BlockingQueue<T> is a thread-safe queue with limited capacity and blocking. It is similar to standard BlockingCollection<T> with queue inside, but take advantage of SemaphoreLight. So the BlockingQueue<T> works up to 5 times faster, especially in heavy load scenarios. Also BlockingQueue<T> has several interesting additional methods.
Sample code:
// Create queue
var queue = new BlockingQueue<int>(boundedCapacity: 100);
int item = 0;
// Enqueue then Dequeue
queue.Add(1);
item = queue.Take();
// Peek methods supported
if (queue.TryPeek(out item))
Console.WriteLine(item);
// Allow to add element even if bouned capacity reached
queue.AddForced(100);
// You can change capacity in runtime
queue.IncreaseBoundedCapacity(1);
queue.DecreaseBoundedCapacity(1);
// Dispose queue
queue.Dispose();
All tests performed on CPU with 8 cores. Every iteration has pause for 10 processor ticks (Thread.Spin(10)).
Add thread count / Take thread count | BlockingQueue | BlockingCollection | Boost |
---|---|---|---|
1 / 1 | 1150 ms | 2020 ms | 1.75x |
4 / 4 | 860 ms | 1900 ms | 2.2x |
16 / 1 | 1290 ms | 16100 ms | 12.48x |
1 / 16 | 1520 ms | 11400 ms | 7.5x |
16 /16 | 910 ms | 2520 ms | 2.77x |