-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreadSafeQueue.cs
48 lines (42 loc) · 1.2 KB
/
ThreadSafeQueue.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System;
using System.Collections;
using System.Collections.Generic;
namespace ShockwaveGORN
{
internal class ThreadSafeQueue<T> : IEnumerable<T>, IEnumerable, ICollection
{
private Queue<T> queue = new Queue<T>();
public int Count { get => queue.Count; }
public object SyncRoot { get => ((ICollection)queue).SyncRoot; }
public bool IsSynchronized { get => true; }
public void Enqueue(T item)
{
lock (SyncRoot)
queue.Enqueue(item);
}
public T Dequeue()
{
if (Count <= 0)
return default;
lock (SyncRoot)
return queue.Dequeue();
}
public void Clear()
{
lock (SyncRoot)
queue.Clear();
}
public void CopyTo(Array array, int index)
{
lock (SyncRoot)
((ICollection)queue).CopyTo(array, index);
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public IEnumerator<T> GetEnumerator()
{
lock (SyncRoot)
foreach (var item in queue)
yield return item;
}
}
}