/// <summary> /// Initializes a new instance of the <see cref="ConcurrentQueue{T}"/> class that contains elements copied /// from the specified collection. /// </summary> /// <param name="collection"> /// The collection whose elements are copied to the new <see cref="ConcurrentQueue{T}"/>. /// </param> /// <exception cref="System.ArgumentNullException">The <paramref name="collection"/> argument is null.</exception> public ConcurrentQueue(IEnumerable <T> collection) { if (collection == null) { throw new ArgumentNullException(nameof(collection)); } _crossSegmentLock = new object(); // Determine the initial segment size. We'll use the default, // unless the collection is known to be larger than that, in which // case we round its length up to a power of 2, as all segments must // be a power of 2 in length. int length = InitialSegmentLength; if (collection is ICollection <T> c) { int count = c.Count; if (count > length) { length = Math.Min(RoundUpToPowerOf2(count), MaxSegmentLength); } } // Initialize the segment and add all of the data to it. _tail = _head = new ConcurrentQueueSegment <T>(length); foreach (T item in collection) { Enqueue(item); } _fixedSize = false; }
/// <summary> /// Initializes a new instance of the <see cref="ConcurrentQueue{T}"/> class. /// </summary> /// <param name="initialCapacity">The initial capacity of the <see cref="ConcurrentQueue{T}"/>.</param> /// <param name="isFixedSize"> /// If <see langword="true"/>, the the <see cref="ConcurrentQueue{T}"/> cannot grow in size. /// Operations on a fixed-size queue are lockless. /// </param> public ConcurrentQueue(int initialCapacity, bool isFixedSize) { initialCapacity = RoundUpToPowerOf2(initialCapacity); _crossSegmentLock = new object(); _tail = _head = new ConcurrentQueueSegment <T>(initialCapacity); _fixedSize = isFixedSize; }