Пример #1
0
        public void Push(TElement element, int timeout = -1)
        {
            if (Finished)
                throw new Exception("Collection finished");

#if DEBUG
            if (element == null)
                throw new Exception();
#endif

            //_writing = true;

            while (!_writerElement.Empty)
            {
                _writerWaiting = true;
                _writerWaitHandle.WaitOne(timeout == -1 ? Timeout : timeout);
            }

            _writerElement.Element = element;
            _writerElement.Empty = false;
            _writerElement = _writerElement.Next;
            _writenCount++;
            //_writing = false;

            if (_readerWaiting)
            {
                _readerWaitHandle.Set();
                _readerWaiting = false;
            }
        }
Пример #2
0
        public void PushRange(IEnumerable<TElement> elements)
        {
            if(Finished)
                throw new Exception("Collection finished");

            foreach (var element in elements)
            {
                while (!Finished && !_writerElement.Empty)
                {
                    _writerWaiting = true;
                    _writerWaitHandle.WaitOne(Timeout);
                }

                if(Finished)
                    throw new Exception("Collection finished");

                _writerElement.Element = element;
                _writerElement.Empty = false;
                _writerElement = _writerElement.Next;
                _writenCount++;

                if (_readerWaiting)
                {
                    _readerWaitHandle.Set();
                    _readerWaiting = false;
                }
            }
        }
Пример #3
0
        public ManagedCircleBuffer(int size, int timeout = 1000)
        {
            Size = size;
            Timeout = timeout;

            var buffer = CircleBuffer<TElement>.BuildBuffer(size);
            _writerElement = buffer;
            _readerElement = buffer;

            _writerWaitHandle = new AutoResetEvent(false);
            _readerWaitHandle = new AutoResetEvent(false);
        }
Пример #4
0
        public bool TryPop(out TElement element, int timeout = -1)
        {
            element = default(TElement);
            if (_readerWaiting || !WaitForElement(timeout))
                return false;

            element = _readerElement.Element;
            _readerElement.Empty = true;
            _readerElement = _readerElement.Next;
            _readedCount++;
            if (_writerWaiting)
            {
                _writerWaitHandle.Set();
                _writerWaiting = false;
            }

            return true;
        }
Пример #5
0
        public IEnumerable<TElement> PopAsEnumerable()
        {
            while (!Finished)
            {
                if (_readerElement.Empty)
                {
                    _readerWaitHandle.WaitOne(Timeout);
                    continue;
                }

                var element = _readerElement.Element;
                //_readerElement.Element = null;
                _readerElement.Empty = true;
                _readerElement = _readerElement.Next;

                _readedCount++;

                yield return element;
            }
        }