/// <summary> /// Attempts to retrieve and remove the next item from the back. /// </summary> /// <param name="item">The item.</param> /// <returns> /// <c>true</c> if the item was taken; otherwise, <c>false</c>. /// </returns> public bool TryTake(out T item) { var spinWait = new SpinWait(); while (true) { var root = Volatile.Read(ref _root); if (root == null) { item = default; return(false); } var found = Interlocked.CompareExchange(ref _root, root.Link, root); if (found == root) { item = root.Value; Node <T> .Donate(root); Interlocked.Decrement(ref _count); return(true); } spinWait.SpinOnce(); } }
/// <summary> /// Attempts to retrieve and remove the next item. /// </summary> /// <param name="item">The item.</param> /// <returns> /// <c>true</c> if the item was taken; otherwise, <c>false</c>. /// </returns> public bool TryTake(out T item) { var spinWait = new SpinWait(); while (true) { var root = Volatile.Read(ref _root); if (root !.Value.TryTake(out item)) { Interlocked.Decrement(ref _count); return(true); } if (root.Link == null) { return(false); } var found = Interlocked.CompareExchange(ref _root, root.Link, root); if (found == root) { Node <FixedSizeQueue <T> > .Donate(root); } spinWait.SpinOnce(); } }