Skip to content

Instantly share code, notes, and snippets.

@jameshulse
Created December 17, 2015 13:40
Show Gist options
  • Save jameshulse/20862c47544b77884dff to your computer and use it in GitHub Desktop.
Save jameshulse/20862c47544b77884dff to your computer and use it in GitHub Desktop.
public class CircularBuffer<T>
{
private readonly int _size;
private readonly T[] _items;
private int _writeLocation = -1;
private readonly object _lock = new object();
public CircularBuffer(int size)
{
_size = size;
_items = new T[size];
}
public void Add(T item)
{
lock (_lock)
{
_writeLocation++;
_writeLocation %= _size;
_items[_writeLocation] = item;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment