Skip to content

Instantly share code, notes, and snippets.

@techgeek1
Last active September 10, 2015 23:47
Show Gist options
  • Save techgeek1/fda6b8c06cf1f55cbf0c to your computer and use it in GitHub Desktop.
Save techgeek1/fda6b8c06cf1f55cbf0c to your computer and use it in GitHub Desktop.
ConcurrentQueue using locks for use in older .NET projects (like in Unity3D)
using System;
using System.Collections.Generic;
namespace KanameExtensions {
public class ConcurrentQueue<T> {
//private vars
private readonly object syncLock = new object();
private Queue<T> rqueue;
//constructors
public ConcurrentQueue() {
rqueue = new Queue<T>();
}
public ConcurrentQueue(IEnumerable<T> collection) {
rqueue = new Queue<T>(collection);
}
public ConcurrentQueue(int capacity) {
rqueue = new Queue<T>(capacity);
}
//properties
public int Count {
get {
lock (syncLock) {
return rqueue.Count;
}
}
}
//methods
public void Clear() {
lock (syncLock) {
rqueue.Clear();
}
}
public bool Contains(T obj) {
lock (syncLock) {
return rqueue.Contains(obj);
}
}
public void CopyTo(T[] array, int index) {
lock (syncLock) {
rqueue.CopyTo(array, index);
}
}
public T Dequeue() {
lock (syncLock) {
return rqueue.Dequeue();
}
}
public void Enqueue(T obj) {
lock (syncLock) {
rqueue.Enqueue(obj);
}
}
public bool Equals(T obj) {
lock (syncLock) {
return rqueue.Equals(obj);
}
}
public Queue<T>.Enumerator GetEnumerator() {
lock (syncLock) {
return rqueue.GetEnumerator();
}
}
public new virtual int GetHashCode() {
lock (syncLock) {
return rqueue.GetHashCode();
}
}
public new Type GetType() {
lock (syncLock) {
return rqueue.GetType();
}
}
public T Peek() {
lock (syncLock) {
return rqueue.Peek();
}
}
public T[] ToArray() {
lock (syncLock) {
return rqueue.ToArray();
}
}
public new virtual string ToString() {
lock (syncLock) {
return rqueue.ToString();
}
}
public void TrimExcess() {
lock (syncLock) {
rqueue.TrimExcess();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment