Example #1
0
        /// <summary>
        ///     Pull any item from buffer. Release a frozen thread which is waiting buffer to become not so overflowed (if any)
        /// </summary>
        public T Pull(out int order)
        {
            if (_orderMatters)
            {
                throw new InvalidOperationException(
                          $"Call another overload of {nameof(Pull)} and provide {nameof(order)}");
            }

            T   returnItem    = default;
            int returnedOrder = default;

            BufferIdea.ThreadSafeAccessToBuffer(WaitersCollection.WaitersMode.OrdersDoesNotMatter(),
                                                () =>
            {
                bool shouldWait = !_internalBuffer.TryRemoveFirst(out returnedOrder, out returnItem);
                return(shouldWait, !shouldWait);
            },
                                                _addWaiters,
                                                _pullWaiters);

            order = returnedOrder;
            return(returnItem);
        }