本文整理汇总了C#中RingBuffer.Read方法的典型用法代码示例。如果您正苦于以下问题:C# RingBuffer.Read方法的具体用法?C# RingBuffer.Read怎么用?C# RingBuffer.Read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RingBuffer
的用法示例。
在下文中一共展示了RingBuffer.Read方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConcurrentReadWrite
public void ConcurrentReadWrite()
{
//Arrange
const int capacity = 128;
RingBuffer<object> ringBuffer = new RingBuffer<object>(capacity, i => new RingBufferItem<object>(i));
AutoResetEvent resetEvent = new AutoResetEvent(false);
Action writerAction = () =>
{
for (int i = 0; i < capacity; i++)
{
int writeIndex = ringBuffer.Write(new object());
Assert.AreEqual(i, writeIndex);
resetEvent.Set();
}
};
Action readerAction = () =>
{
for (int i = 0; i < capacity; i++)
{
int readIndex;
object read = ringBuffer.Read(out readIndex);
if (readIndex < 0)
{
resetEvent.WaitOne(100);
read = ringBuffer.Read(out readIndex);
}
Assert.AreEqual(i, readIndex, "readIndex does not equal loop index.");
Assert.IsNotNull(read, "read object in null");
}
};
Parallel.Invoke(writerAction, readerAction);
}
示例2: FillBufferThenReadHalfThenWriteHalf
public void FillBufferThenReadHalfThenWriteHalf()
{
//Arrange
const int capacity = 256;
RingBuffer<object> ringBuffer = new RingBuffer<object>(capacity, i => new RingBufferItem<object>(i));
Assert.AreEqual(256, ringBuffer.Capacity);
//act
for (int i = 0; i < capacity; i++)
{
var indexUsed = ringBuffer.Write(new object());
Assert.AreEqual(i, indexUsed, "The write index should match the loop index");
}
//The buffer should be full, so no more writes are allowed
var index = ringBuffer.Write(new object());
Assert.AreEqual(-1, index);
for (int i = 0; i < capacity / 2; i++)
{
int readIndex;
var item = ringBuffer.Read(out readIndex);
Assert.IsNotNull(item, "Item should not be null");
Assert.AreEqual(i, readIndex, "The Read index should be same as the loop index");
}
//Write again from the beginning
for (int i = 0; i < capacity / 2; i++)
{
var indexUsed = ringBuffer.Write(new object());
Assert.AreEqual(i, indexUsed, "The write index should match the loop index");
}
//Assert
index = ringBuffer.Write(new object());
Assert.AreEqual(-1, index);
}
示例3: ReadEmptyBuffer
public void ReadEmptyBuffer()
{
//Arrange
const int capacity = 64;
RingBuffer<object> ringBuffer = new RingBuffer<object>(capacity, i => new RingBufferItem<object>(i));
Assert.AreEqual(64, ringBuffer.Capacity);
//Act
int readIndex;
object read = ringBuffer.Read(out readIndex);
//Assert
Assert.IsNull(read, "The read object should be null");
Assert.AreEqual(-1, readIndex, "readIndex should not be positive");
}
示例4: FillHalfTheBufferThenReadAll
public void FillHalfTheBufferThenReadAll()
{
//Arrange
const int capacity = 256;
RingBuffer<object> ringBuffer = new RingBuffer<object>(capacity, index => new RingBufferItem<object>(index));
Assert.AreEqual(256, ringBuffer.Capacity);
//act
for (int j = 0; j < capacity / 2; j++)
{
var indexUsed = ringBuffer.Write(new object());
Assert.AreEqual(j, indexUsed, "The write index should match the loop index");
}
int i;
int readIndex = 0;
for (i = 0; i < capacity; i++)
{
var item = ringBuffer.Read(out readIndex);
if (readIndex == -1)
break;
Assert.IsNotNull(item);
Assert.AreEqual(i, readIndex, "The Read index should be same as the loop index");
}
//Assert
Assert.AreEqual(-1, readIndex);
Assert.AreEqual(capacity / 2, i);
}