MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
BlockingQueue.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading;
6using MyCaffe.basecode;
7
8namespace MyCaffe.common
9{
14 public class BlockingQueue<T> : IDisposable
15 {
16 CancelEvent m_evtCancel;
17 ManualResetEvent m_evtReady = new ManualResetEvent(false);
18 ManualResetEvent m_evtAbort = new ManualResetEvent(false);
19 object m_syncObj = new object();
20 List<T> m_rgData = new List<T>();
21 int m_nDataCount = 0;
22
27 public BlockingQueue(CancelEvent evtCancel)
28 {
29 m_evtCancel = evtCancel;
30 }
31
35 public void Reset()
36 {
37 m_evtAbort.Reset();
38 }
39
43 public void Abort()
44 {
45 m_evtAbort.Set();
46 }
47
51 public int Count
52 {
53 get { return m_nDataCount; }
54 }
55
62 public bool GetAt(int nIdx, ref T t)
63 {
64 while (Count <= nIdx)
65 {
66 if (m_evtCancel == null)
67 return false;
68
69 WaitHandle[] rghCancel = m_evtCancel.Handles;
70
71 if (m_evtReady == null || m_evtAbort == null)
72 return false;
73
74 ManualResetEvent evtReady = m_evtReady;
75 ManualResetEvent evtAbort = m_evtAbort;
76
77 List<WaitHandle> rgWait = new List<WaitHandle>();
78 rgWait.AddRange(rghCancel);
79 rgWait.Add(evtAbort);
80 rgWait.Add(evtReady);
81
82 int nWait = WaitHandle.WaitAny(rgWait.ToArray());
83 if (nWait < rgWait.Count - 1)
84 {
85 evtAbort.Reset();
86 return false;
87 }
88
89 evtReady.Reset();
90 }
91
92 lock (m_syncObj)
93 {
94 t = m_rgData[nIdx];
95 return true;
96 }
97 }
98
102 public void Clear()
103 {
104 lock (m_syncObj)
105 {
106 m_rgData.Clear();
107 m_nDataCount = 0;
108 }
109 }
110
115 public void Push(T t)
116 {
117 lock (m_syncObj)
118 {
119 if (m_evtReady != null)
120 {
121 m_rgData.Add(t);
122 m_nDataCount = m_rgData.Count;
123 m_evtReady.Set();
124 }
125 }
126 }
127
136 public bool Pop(ref T t)
137 {
138 while (Count == 0)
139 {
140 WaitHandle[] rghCancel = m_evtCancel.Handles;
141
142 if (m_evtCancel == null || m_evtAbort == null)
143 return false;
144
145 ManualResetEvent evtAbort = m_evtAbort;
146 ManualResetEvent evtReady = m_evtReady;
147
148 List<WaitHandle> rgWait = new List<WaitHandle>();
149 rgWait.AddRange(rghCancel);
150 rgWait.Add(evtAbort);
151 rgWait.Add(evtReady);
152
153 int nWait = WaitHandle.WaitAny(rgWait.ToArray());
154 if (nWait < rgWait.Count - 1)
155 return false;
156
157 evtReady.Reset();
158 }
159
160 lock (m_syncObj)
161 {
162 t = m_rgData[0];
163 m_rgData.RemoveAt(0);
164 m_nDataCount = m_rgData.Count;
165 return true;
166 }
167 }
168
177 public bool Peek(ref T t)
178 {
179 while (Count == 0)
180 {
181 if (m_evtReady == null)
182 return false;
183
184 if (m_evtCancel.WaitOne(0))
185 return false;
186
187 Thread.Sleep(0);
188 }
189
190 lock (m_syncObj)
191 {
192 t = m_rgData[0];
193 return true;
194 }
195 }
196
201 protected virtual void Dispose(bool bDisposing)
202 {
203 if (m_evtAbort != null)
204 {
205 m_evtAbort.Set();
206 Thread.Sleep(50);
207 m_evtAbort.Dispose();
208 m_evtAbort = null;
209 }
210
211 if (m_evtReady != null)
212 {
213 m_evtReady.Dispose();
214 m_evtReady = null;
215 }
216
217 Clear();
218 }
219
223 public void Dispose()
224 {
225 Dispose(true);
226 }
227 }
228}
The CancelEvent provides an extension to the manual cancel event that allows for overriding the manua...
Definition: CancelEvent.cs:17
The BlockingQueue is used for synchronized Queue operations.
bool Pop(ref T t)
Remove an item from the front of the queue.
BlockingQueue(CancelEvent evtCancel)
The BlockingQueue constructor.
virtual void Dispose(bool bDisposing)
Release all resources used by the queue.
void Reset()
Reset the abort event.
bool Peek(ref T t)
Retrieve an item from the front of the queue, but do not remove it.
void Abort()
Cancel the blocking queue operations.
void Clear()
Remove all items from the queue.
bool GetAt(int nIdx, ref T t)
Returns the item at a given index within the queue.
void Push(T t)
Add an item to the back of the queue.
void Dispose()
Release all resources used by the queue.
int Count
Return the number of items in the queue.
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12
The MyCaffe.common namespace contains common MyCaffe classes.
Definition: BatchInput.cs:8
The MyCaffe namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...
Definition: Annotation.cs:12