ConcurrentQueue Class

Definition

Represents a thread-safe first in-first out (FIFO) collection.

[System.Runtime.InteropServices.ComVisible(false)]
public class ConcurrentQueue<T> : System.Collections.Concurrent.IProducerConsumerCollection<T>, System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IReadOnlyCollection<T>
Type Parameters
T

The type of the elements contained in the queue.

Inheritance
ConcurrentQueue<T>
Attributes
Implements

Inherited Members

System.Object

Examples

The following example shows how to use a ConcurrentQueue<T> to enqueue and dequeue items:

using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;

class CQ_EnqueueDequeuePeek
{
   // Demonstrates:
   // ConcurrentQueue<T>.Enqueue()
   // ConcurrentQueue<T>.TryPeek()
   // ConcurrentQueue<T>.TryDequeue()
   static void Main ()
   {
      // Construct a ConcurrentQueue.
      ConcurrentQueue<int> cq = new ConcurrentQueue<int>();

      // Populate the queue.
      for (int i = 0; i < 10000; i++) cq.Enqueue(i);

      // Peek at the first element.
      int result;
      if (!cq.TryPeek(out result))
      {
         Console.WriteLine("CQ: TryPeek failed when it should have succeeded");
      }
      else if (result != 0)
      {
         Console.WriteLine("CQ: Expected TryPeek result of 0, got {0}", result);
      }

      int outerSum = 0;
      // An action to consume the ConcurrentQueue.
      Action action = () =>
      {                
         int localSum = 0;
         int localValue;
         while (cq.TryDequeue(out localValue)) localSum += localValue;
         Interlocked.Add(ref outerSum, localSum);
      };

      // Start 4 concurrent consuming actions.
      Parallel.Invoke(action, action, action, action);

      Console.WriteLine("outerSum = {0}, should be 49995000", outerSum);
   }
}
Imports System.Collections.Concurrent
Imports System.Threading
Imports System.Threading.Tasks

Class TestQueue
   ' Demonstrates:
   ' ConcurrentQueue<T>.Enqueue()
   ' ConcurrentQueue<T>.TryPeek()
   ' ConcurrentQueue<T>.TryDequeue()
   Shared Sub Main()
      ' Construct a ConcurrentQueue
      Dim cq As New ConcurrentQueue(Of Integer)()

      ' Populate the queue
      For i As Integer = 0 To 9999
         cq.Enqueue(i)
      Next

      ' Peek at the first element
      Dim result As Integer
      If Not cq.TryPeek(result) Then
         Console.WriteLine("CQ: TryPeek failed when it should have succeeded")
      ElseIf result <> 0 Then
         Console.WriteLine("CQ: Expected TryPeek result of 0, got {0}", result)
      End If

      Dim outerSum As Integer = 0

      ' An action to consume the ConcurrentQueue
      Dim action As Action =
          Sub()
             Dim localSum As Integer = 0
             Dim localValue As Integer
             While cq.TryDequeue(localValue)
                localSum += localValue
             End While
             Interlocked.Add(outerSum, localSum)
          End Sub

      ' Start 4 concurrent consuming actions
      Parallel.Invoke(action, action, action, action)

      Console.WriteLine("outerSum = {0}, should be 49995000", outerSum)
   End Sub
End Class

Remarks

Note

ConcurrentQueue<T> implements the IReadOnlyCollection<T> interface starting with the .NET Framework 4.6; in previous versions of the .NET Framework, the ConcurrentQueue<T> class did not implement this interface.

Constructors

ConcurrentQueue<T>()

Initializes a new instance of the ConcurrentQueue<T> class.

ConcurrentQueue<T>(IEnumerable<T>)

Initializes a new instance of the ConcurrentQueue<T> class that contains elements copied from the specified collection

Properties

Count

Gets the number of elements contained in the ConcurrentQueue<T>.

IsEmpty

Gets a value that indicates whether the ConcurrentQueue<T> is empty.

Methods

Clear()
CopyTo(T[], Int32)

Copies the ConcurrentQueue<T> elements to an existing one-dimensional Array, starting at the specified array index.

Enqueue(T)

Adds an object to the end of the ConcurrentQueue<T>.

GetEnumerator()

Returns an enumerator that iterates through the ConcurrentQueue<T>.

ToArray()

Copies the elements stored in the ConcurrentQueue<T> to a new array.

TryDequeue(T)

Tries to remove and return the object at the beginning of the concurrent queue.

TryPeek(T)

Tries to return an object from the beginning of the ConcurrentQueue<T> without removing it.

Explicit Interface Implementations

IProducerConsumerCollection<T>.TryAdd(T)

Attempts to add an object to the IProducerConsumerCollection<T>.

IProducerConsumerCollection<T>.TryTake(T)

Attempts to remove and return an object from the IProducerConsumerCollection<T>.

ICollection.CopyTo(Array, Int32)

Copies the elements of the ICollection to an Array, starting at a particular Array index.

ICollection.IsSynchronized

Gets a value indicating whether access to the ICollection is synchronized with the SyncRoot.

ICollection.SyncRoot

Gets an object that can be used to synchronize access to the ICollection. This property is not supported.

IEnumerable.GetEnumerator()

Returns an enumerator that iterates through a collection.

Extension Methods

ToImmutableArray<TSource>(IEnumerable<TSource>)
ToImmutableDictionary<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>)
ToImmutableDictionary<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>, IEqualityComparer<TKey>)
ToImmutableDictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>)
ToImmutableDictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>, IEqualityComparer<TKey>)
ToImmutableDictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>, IEqualityComparer<TKey>, IEqualityComparer<TValue>)
ToImmutableDictionary<TSource,TKey,TValue>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TValue>)
ToImmutableDictionary<TSource,TKey,TValue>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TValue>, IEqualityComparer<TKey>)
ToImmutableDictionary<TSource,TKey,TValue>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TValue>, IEqualityComparer<TKey>, IEqualityComparer<TValue>)
ToImmutableHashSet<TSource>(IEnumerable<TSource>)
ToImmutableHashSet<TSource>(IEnumerable<TSource>, IEqualityComparer<TSource>)
ToImmutableList<TSource>(IEnumerable<TSource>)
ToImmutableSortedDictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>)
ToImmutableSortedDictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>, IComparer<TKey>)
ToImmutableSortedDictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>, IComparer<TKey>, IEqualityComparer<TValue>)
ToImmutableSortedDictionary<TSource,TKey,TValue>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TValue>)
ToImmutableSortedDictionary<TSource,TKey,TValue>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TValue>, IComparer<TKey>)
ToImmutableSortedDictionary<TSource,TKey,TValue>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TValue>, IComparer<TKey>, IEqualityComparer<TValue>)
ToImmutableSortedSet<TSource>(IEnumerable<TSource>)
ToImmutableSortedSet<TSource>(IEnumerable<TSource>, IComparer<TSource>)
CopyToDataTable<T>(IEnumerable<T>)
CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption)
CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler)
Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>)
Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>)
Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>)
All<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
Any<TSource>(IEnumerable<TSource>)
Any<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
Append<TSource>(IEnumerable<TSource>, TSource)
AsEnumerable<TSource>(IEnumerable<TSource>)
Average(IEnumerable<Decimal>)
Average(IEnumerable<Double>)
Average(IEnumerable<Int32>)
Average(IEnumerable<Int64>)
Average(IEnumerable<Nullable<Decimal>>)
Average(IEnumerable<Nullable<Double>>)
Average(IEnumerable<Nullable<Int32>>)
Average(IEnumerable<Nullable<Int64>>)
Average(IEnumerable<Nullable<Single>>)
Average(IEnumerable<Single>)
Average<TSource>(IEnumerable<TSource>, Func<TSource,Decimal>)
Average<TSource>(IEnumerable<TSource>, Func<TSource,Double>)
Average<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
Average<TSource>(IEnumerable<TSource>, Func<TSource,Int64>)
Average<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Decimal>>)
Average<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Double>>)
Average<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int32>>)
Average<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int64>>)
Average<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Single>>)
Average<TSource>(IEnumerable<TSource>, Func<TSource,Single>)
Cast<TResult>(IEnumerable)
Concat<TSource>(IEnumerable<TSource>, IEnumerable<TSource>)
Contains<TSource>(IEnumerable<TSource>, TSource)
Contains<TSource>(IEnumerable<TSource>, TSource, IEqualityComparer<TSource>)
Count<TSource>(IEnumerable<TSource>)
Count<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
DefaultIfEmpty<TSource>(IEnumerable<TSource>)
DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource)
Distinct<TSource>(IEnumerable<TSource>)
Distinct<TSource>(IEnumerable<TSource>, IEqualityComparer<TSource>)
ElementAt<TSource>(IEnumerable<TSource>, Int32)
ElementAtOrDefault<TSource>(IEnumerable<TSource>, Int32)
Except<TSource>(IEnumerable<TSource>, IEnumerable<TSource>)
Except<TSource>(IEnumerable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>)
First<TSource>(IEnumerable<TSource>)
First<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
FirstOrDefault<TSource>(IEnumerable<TSource>)
FirstOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>)
GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>, IEqualityComparer<TKey>)
GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>)
GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, IEqualityComparer<TKey>)
GroupBy<TSource,TKey,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TKey,IEnumerable<TSource>,TResult>)
GroupBy<TSource,TKey,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TKey,IEnumerable<TSource>,TResult>, IEqualityComparer<TKey>)
GroupBy<TSource,TKey,TElement,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, Func<TKey,IEnumerable<TElement>,TResult>)
GroupBy<TSource,TKey,TElement,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, Func<TKey,IEnumerable<TElement>,TResult>, IEqualityComparer<TKey>)
GroupJoin<TOuter,TInner,TKey,TResult>(IEnumerable<TOuter>, IEnumerable<TInner>, Func<TOuter,TKey>, Func<TInner,TKey>, Func<TOuter,IEnumerable<TInner>,TResult>)
GroupJoin<TOuter,TInner,TKey,TResult>(IEnumerable<TOuter>, IEnumerable<TInner>, Func<TOuter,TKey>, Func<TInner,TKey>, Func<TOuter,IEnumerable<TInner>,TResult>, IEqualityComparer<TKey>)
Intersect<TSource>(IEnumerable<TSource>, IEnumerable<TSource>)
Intersect<TSource>(IEnumerable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>)
Join<TOuter,TInner,TKey,TResult>(IEnumerable<TOuter>, IEnumerable<TInner>, Func<TOuter,TKey>, Func<TInner,TKey>, Func<TOuter,TInner,TResult>)
Join<TOuter,TInner,TKey,TResult>(IEnumerable<TOuter>, IEnumerable<TInner>, Func<TOuter,TKey>, Func<TInner,TKey>, Func<TOuter,TInner,TResult>, IEqualityComparer<TKey>)
Last<TSource>(IEnumerable<TSource>)
Last<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
LastOrDefault<TSource>(IEnumerable<TSource>)
LastOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
LongCount<TSource>(IEnumerable<TSource>)
LongCount<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
Max(IEnumerable<Decimal>)
Max(IEnumerable<Double>)
Max(IEnumerable<Int32>)
Max(IEnumerable<Int64>)
Max(IEnumerable<Nullable<Decimal>>)
Max(IEnumerable<Nullable<Double>>)
Max(IEnumerable<Nullable<Int32>>)
Max(IEnumerable<Nullable<Int64>>)
Max(IEnumerable<Nullable<Single>>)
Max(IEnumerable<Single>)
Max<TSource>(IEnumerable<TSource>)
Max<TSource>(IEnumerable<TSource>, Func<TSource,Decimal>)
Max<TSource>(IEnumerable<TSource>, Func<TSource,Double>)
Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
Max<TSource>(IEnumerable<TSource>, Func<TSource,Int64>)
Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Decimal>>)
Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Double>>)
Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int32>>)
Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int64>>)
Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Single>>)
Max<TSource>(IEnumerable<TSource>, Func<TSource,Single>)
Max<TSource,TResult>(IEnumerable<TSource>, Func<TSource,TResult>)
Min(IEnumerable<Decimal>)
Min(IEnumerable<Double>)
Min(IEnumerable<Int32>)
Min(IEnumerable<Int64>)
Min(IEnumerable<Nullable<Decimal>>)
Min(IEnumerable<Nullable<Double>>)
Min(IEnumerable<Nullable<Int32>>)
Min(IEnumerable<Nullable<Int64>>)
Min(IEnumerable<Nullable<Single>>)
Min(IEnumerable<Single>)
Min<TSource>(IEnumerable<TSource>)
Min<TSource>(IEnumerable<TSource>, Func<TSource,Decimal>)
Min<TSource>(IEnumerable<TSource>, Func<TSource,Double>)
Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
Min<TSource>(IEnumerable<TSource>, Func<TSource,Int64>)
Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Decimal>>)
Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Double>>)
Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int32>>)
Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int64>>)
Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Single>>)
Min<TSource>(IEnumerable<TSource>, Func<TSource,Single>)
Min<TSource,TResult>(IEnumerable<TSource>, Func<TSource,TResult>)
OfType<TResult>(IEnumerable)
OrderBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>)
OrderBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>, IComparer<TKey>)
OrderByDescending<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>)
OrderByDescending<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>, IComparer<TKey>)
Prepend<TSource>(IEnumerable<TSource>, TSource)
Reverse<TSource>(IEnumerable<TSource>)
Select<TSource,TResult>(IEnumerable<TSource>, Func<TSource,TResult>)
Select<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,TResult>)
SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>)
SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>)
SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)
SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)
SequenceEqual<TSource>(IEnumerable<TSource>, IEnumerable<TSource>)
SequenceEqual<TSource>(IEnumerable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>)
Single<TSource>(IEnumerable<TSource>)
Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
SingleOrDefault<TSource>(IEnumerable<TSource>)
SingleOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
Skip<TSource>(IEnumerable<TSource>, Int32)
SkipLast<TSource>(IEnumerable<TSource>, Int32)
SkipWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
SkipWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>)
Sum(IEnumerable<Decimal>)
Sum(IEnumerable<Double>)
Sum(IEnumerable<Int32>)
Sum(IEnumerable<Int64>)
Sum(IEnumerable<Nullable<Decimal>>)
Sum(IEnumerable<Nullable<Double>>)
Sum(IEnumerable<Nullable<Int32>>)
Sum(IEnumerable<Nullable<Int64>>)
Sum(IEnumerable<Nullable<Single>>)
Sum(IEnumerable<Single>)
Sum<TSource>(IEnumerable<TSource>, Func<TSource,Decimal>)
Sum<TSource>(IEnumerable<TSource>, Func<TSource,Double>)
Sum<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
Sum<TSource>(IEnumerable<TSource>, Func<TSource,Int64>)
Sum<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Decimal>>)
Sum<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Double>>)
Sum<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int32>>)
Sum<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int64>>)
Sum<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Single>>)
Sum<TSource>(IEnumerable<TSource>, Func<TSource,Single>)
Take<TSource>(IEnumerable<TSource>, Int32)
TakeLast<TSource>(IEnumerable<TSource>, Int32)
TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>)
ToArray<TSource>(IEnumerable<TSource>)
ToDictionary<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>)
ToDictionary<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>, IEqualityComparer<TKey>)
ToDictionary<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>)
ToDictionary<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, IEqualityComparer<TKey>)
ToHashSet<TSource>(IEnumerable<TSource>)
ToHashSet<TSource>(IEnumerable<TSource>, IEqualityComparer<TSource>)
ToList<TSource>(IEnumerable<TSource>)
ToLookup<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>)
ToLookup<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>, IEqualityComparer<TKey>)
ToLookup<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>)
ToLookup<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, IEqualityComparer<TKey>)
Union<TSource>(IEnumerable<TSource>, IEnumerable<TSource>)
Union<TSource>(IEnumerable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>)
Where<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
Where<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>)
Zip<TFirst,TSecond,TResult>(IEnumerable<TFirst>, IEnumerable<TSecond>, Func<TFirst,TSecond,TResult>)
AsParallel(IEnumerable)
AsParallel<TSource>(IEnumerable<TSource>)
AsQueryable(IEnumerable)
AsQueryable<TElement>(IEnumerable<TElement>)
Ancestors<T>(IEnumerable<T>)
Ancestors<T>(IEnumerable<T>, XName)
AncestorsAndSelf(IEnumerable<XElement>)
AncestorsAndSelf(IEnumerable<XElement>, XName)
Attributes(IEnumerable<XElement>)
Attributes(IEnumerable<XElement>, XName)
DescendantNodes<T>(IEnumerable<T>)
DescendantNodesAndSelf(IEnumerable<XElement>)
Descendants<T>(IEnumerable<T>)
Descendants<T>(IEnumerable<T>, XName)
DescendantsAndSelf(IEnumerable<XElement>)
DescendantsAndSelf(IEnumerable<XElement>, XName)
Elements<T>(IEnumerable<T>)
Elements<T>(IEnumerable<T>, XName)
InDocumentOrder<T>(IEnumerable<T>)
Nodes<T>(IEnumerable<T>)
Remove(IEnumerable<XAttribute>)
Remove<T>(IEnumerable<T>)

Thread Safety

All public and protected members of ConcurrentQueue<T> are thread-safe and may be used concurrently from multiple threads.