ConcurrentQueue<T> Class
Definition
Represents a thread-safe first in-first out (FIFO) collection.
generic <typename T>
public ref class ConcurrentQueue : System::Collections::Concurrent::IProducerConsumerCollection<T>, System::Collections::Generic::IEnumerable<T>, System::Collections::Generic::IReadOnlyCollection<T>
[System.Runtime.InteropServices.ComVisible(false)]
[System.Serializable]
public class ConcurrentQueue<T> : System.Collections.Concurrent.IProducerConsumerCollection<T>, System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IReadOnlyCollection<T>
type ConcurrentQueue<'T> = class
interface IProducerConsumerCollection<'T>
interface IReadOnlyCollection<'T>
interface seq<'T>
interface ICollection
interface IEnumerable
Public Class ConcurrentQueue(Of T)
Implements IEnumerable(Of T), IProducerConsumerCollection(Of T), IReadOnlyCollection(Of T)
Type Parameters
- T
The type of the elements contained in the queue.
- Inheritance
-
ConcurrentQueue<T>
- Attributes
- Implements
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() |
Removes all objects from the ConcurrentQueue<T>. |
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>. |
Equals(Object) |
Determines whether the specified object is equal to the current object. (Inherited from Object) |
GetEnumerator() |
Returns an enumerator that iterates through the ConcurrentQueue<T>. |
GetHashCode() |
Serves as the default hash function. (Inherited from Object) |
GetType() |
Gets the Type of the current instance. (Inherited from Object) |
MemberwiseClone() |
Creates a shallow copy of the current Object. (Inherited from Object) |
ToArray() |
Copies the elements stored in the ConcurrentQueue<T> to a new array. |
ToString() |
Returns a string that represents the current object. (Inherited from Object) |
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
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. |
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>. |
Extension Methods
CopyToDataTable<T>(IEnumerable<T>) |
Returns a DataTable that contains copies of the DataRow objects, given an input IEnumerable<T> object where the generic parameter |
CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption) |
Copies DataRow objects to the specified DataTable, given an input IEnumerable<T> object where the generic parameter |
CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler) |
Copies DataRow objects to the specified DataTable, given an input IEnumerable<T> object where the generic parameter |
Cast<TResult>(IEnumerable) |
Casts the elements of an IEnumerable to the specified type. |
OfType<TResult>(IEnumerable) |
Filters the elements of an IEnumerable based on a specified type. |
AsParallel(IEnumerable) |
Enables parallelization of a query. |
AsQueryable(IEnumerable) |
Converts an IEnumerable to an IQueryable. |
Ancestors<T>(IEnumerable<T>) |
Returns a collection of elements that contains the ancestors of every node in the source collection. |
Ancestors<T>(IEnumerable<T>, XName) |
Returns a filtered collection of elements that contains the ancestors of every node in the source collection. Only elements that have a matching XName are included in the collection. |
DescendantNodes<T>(IEnumerable<T>) |
Returns a collection of the descendant nodes of every document and element in the source collection. |
Descendants<T>(IEnumerable<T>) |
Returns a collection of elements that contains the descendant elements of every element and document in the source collection. |
Descendants<T>(IEnumerable<T>, XName) |
Returns a filtered collection of elements that contains the descendant elements of every element and document in the source collection. Only elements that have a matching XName are included in the collection. |
Elements<T>(IEnumerable<T>) |
Returns a collection of the child elements of every element and document in the source collection. |
Elements<T>(IEnumerable<T>, XName) |
Returns a filtered collection of the child elements of every element and document in the source collection. Only elements that have a matching XName are included in the collection. |
InDocumentOrder<T>(IEnumerable<T>) |
Returns a collection of nodes that contains all nodes in the source collection, sorted in document order. |
Nodes<T>(IEnumerable<T>) |
Returns a collection of the child nodes of every document and element in the source collection. |
Remove<T>(IEnumerable<T>) |
Removes every node in the source collection from its parent node. |
Applies to
Thread Safety
All public and protected members of ConcurrentQueue<T> are thread-safe and may be used concurrently from multiple threads.