MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
DataItemCollection.cs
1using MyCaffe.basecode;
2using System;
3using System.Collections.Generic;
4using System.Diagnostics;
5using System.Linq;
6using System.Text;
7using System.Threading;
8using System.Threading.Tasks;
9
10namespace MyCaffe.db.stream
11{
15 public class DataItemCollection
16 {
17 object m_objSync = new object();
18 List<DataItem> m_rgItems = new List<DataItem>();
19 ManualResetEvent m_evtDataReady = new ManualResetEvent(false);
20 ManualResetEvent m_evtAtCount = new ManualResetEvent(false);
21 ManualResetEvent m_evtQueryEnd = new ManualResetEvent(false);
22 CancelEvent m_evtCancel = new CancelEvent();
23 int m_nAtCount = 0;
24
29 public DataItemCollection(int nAtCount)
30 {
31 m_nAtCount = nAtCount;
32 }
33
37 public int Count
38 {
39 get { return m_rgItems.Count; }
40 }
41
46 {
47 get { return m_evtCancel; }
48 }
49
53 public ManualResetEvent QueryEnd
54 {
55 get { return m_evtQueryEnd; }
56 }
57
64 public bool WaitForCount(int nWait)
65 {
66 List<WaitHandle> rgWait = new List<WaitHandle>();
67 rgWait.AddRange(m_evtCancel.Handles);
68 rgWait.Add(m_evtAtCount);
69 rgWait.Add(m_evtQueryEnd);
70 WaitHandle[] rgWait1 = rgWait.ToArray();
71
72 int nWaitItem = WaitHandle.WaitAny(rgWait1, 10);
73 if (nWaitItem == rgWait.Count - 2)
74 return true;
75
76 Stopwatch sw = new Stopwatch();
77 sw.Start();
78
79 while (nWaitItem >= m_evtCancel.Handles.Length)
80 {
81 if (nWaitItem == rgWait.Count - 2)
82 {
83 return true;
84 }
85 else if (nWaitItem == rgWait.Count - 1)
86 {
87 lock (m_objSync)
88 {
89 if (m_rgItems.Count == 0)
90 return false;
91 }
92 }
93
94 if (sw.Elapsed.TotalMilliseconds > nWait)
95 return false;
96
97 nWaitItem = WaitHandle.WaitAny(rgWait1, 10);
98 }
99
100 return false;
101 }
102
107 public void Add(DataItem di)
108 {
109 lock (m_objSync)
110 {
111 m_rgItems.Add(di);
112 m_evtDataReady.Set();
113
114 if (m_rgItems.Count >= m_nAtCount)
115 m_evtAtCount.Set();
116 }
117 }
118
124 public DataItem GetData(int nWait)
125 {
126 if (!m_evtDataReady.WaitOne(nWait))
127 return null;
128
129 lock (m_objSync)
130 {
131 DataItem di = m_rgItems[0];
132 m_rgItems.RemoveAt(0);
133
134 if (m_rgItems.Count == 0)
135 m_evtDataReady.Reset();
136
137 if (m_rgItems.Count < m_nAtCount)
138 m_evtAtCount.Reset();
139
140 return di;
141 }
142 }
143
149 public bool WaitData(int nWait)
150 {
151 return m_evtDataReady.WaitOne(nWait);
152 }
153
157 public void Clear()
158 {
159 lock (m_objSync)
160 {
161 m_rgItems.Clear();
162 m_evtQueryEnd.Reset();
163 m_evtDataReady.Reset();
164 m_evtAtCount.Reset();
165 }
166 }
167 }
168}
The CancelEvent provides an extension to the manual cancel event that allows for overriding the manua...
Definition: CancelEvent.cs:17
WaitHandle[] Handles
Returns the internal wait handle of the CancelEvent.
Definition: CancelEvent.cs:302
The DataItemCollection contains the collection of synchronized data items collected from all custom q...
DataItem GetData(int nWait)
Returns the next data item from the back of the queue.
CancelEvent Cancel
Cancels the internal WaitForCount.
ManualResetEvent QueryEnd
The QueryEnd is set when the data reaches the data end.
DataItemCollection(int nAtCount)
The constructor.
int Count
Returns the number of items in the queue.
bool WaitForCount(int nWait)
The WaitForCount function waits for the data queue to either fill to a given number of items (e....
void Add(DataItem di)
Add a new data item to the queue.
void Clear()
The Clear method removes all data from the data queue.
bool WaitData(int nWait)
The WaitData function waits a given amount of time for data to be ready.
The DataItem manages one synchronized data item where the first element is the sync field.
Definition: DataItem.cs:13
void Reset()
Clears the data fields and the filled status.
Definition: DataItem.cs:80
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12
The MyCaffe.db.stream namespace contains all data streaming related classes.
The MyCaffe namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...
Definition: Annotation.cs:12