/// <summary>
    /// Removes an item from the buffer.
    /// </summary>
    /// <returns>The next available item</returns>
    public T Dequeue()
    {
        var next = _consumerCursor.ReadAcquireFence() + 1;

        while (_producerCursor.ReadAcquireFence() < next) // makes sure we read the data from _entries after we have read the producer cursor
        {
            Thread.SpinWait(1);
        }
        var result = this[next];

        _consumerCursor.WriteReleaseFence(next); // makes sure we read the data from _entries before we update the consumer cursor
        return(result);
    }
    /// <summary>
    /// Add an item to the buffer
    /// </summary>
    /// <param name="item"></param>
    public void Enqueue(T item)
    {
        var next = _producerCursor.ReadAcquireFence() + 1;

        long wrapPoint = next - _entries.Length;
        long min       = _consumerCursor.ReadAcquireFence();

        while (wrapPoint > min)
        {
            min = _consumerCursor.ReadAcquireFence();
            Thread.SpinWait(1);
        }

        this[next] = item;
        _producerCursor.WriteReleaseFence(next); // makes sure we write the data in _entries before we update the producer cursor
    }
Exemple #3
0
 public void WriteReleaseFenceChangesInitialValue()
 {
     _volatile.WriteReleaseFence(NewValue);
     Assert.AreEqual(NewValue, _volatile.ReadUnfenced());
 }