MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
InternalThread.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using System.Threading;
7using MyCaffe.basecode;
8
9namespace MyCaffe.common
10{
15 public class InternalThread<T> : IDisposable
16 {
17 Task m_task = null;
18 Thread m_thread = null;
19 CancelEvent m_evtCancel = new CancelEvent();
20 AutoResetEvent m_evtDone = new AutoResetEvent(false);
21 ManualResetEvent m_evtRunning = new ManualResetEvent(false);
22 ManualResetEvent m_evtAbort = new ManualResetEvent(false);
23 bool m_bUseThread = true;
24
28 public event EventHandler<ActionStateArgs<T>> DoWork;
32 public event EventHandler OnPreStop;
36 public event EventHandler OnPreStart;
37
42 public InternalThread(bool bUseThreadVsTask = false)
43 {
44 m_bUseThread = bUseThreadVsTask;
45 }
46
51 protected virtual void Dispose(bool bDisposing)
52 {
53 if (m_evtCancel != null)
54 m_evtCancel.Set();
55
57
58 if (m_evtCancel != null)
59 {
60 m_evtCancel.Dispose();
61 m_evtCancel = null;
62 }
63 }
64
68 public void Dispose()
69 {
70 Dispose(true);
71 }
72
81 public void StartInternalThread(CudaDnn<T> cuda, Log log, int nDeviceID = 0, object arg = null, int nInitialDelay = 0)
82 {
83 m_evtAbort.Reset();
84 m_evtCancel.Reset();
85
86 if (OnPreStart != null)
87 OnPreStart(this, new EventArgs());
88
89 if (m_bUseThread)
90 {
91 if (m_thread == null)
92 {
93 m_thread = new Thread(new ParameterizedThreadStart(InternalThreadEntry));
94 m_thread.Start(new ActionStateArgs<T>(cuda, log, m_evtCancel, nDeviceID, arg, nInitialDelay));
95 }
96 }
97 else
98 {
99 if (m_task == null)
100 {
101 Action<object> action = new Action<object>(InternalThreadEntry);
102 m_task = Task.Factory.StartNew(action, new ActionStateArgs<T>(cuda, log, m_evtCancel, nDeviceID, arg), TaskCreationOptions.LongRunning);
103 }
104 }
105 }
106
107 private void waitForTerminate()
108 {
109 m_evtAbort.Set();
110 if (!m_evtDone.WaitOne(10000))
111 throw new Exception("The Internal Thread (thread) failed to stop!");
112 }
113
117 public void StopInternalThread()
118 {
119 if (OnPreStop != null)
120 OnPreStop(this, new EventArgs());
121
122 if (m_thread != null)
123 {
124 m_evtCancel.Set();
125 waitForTerminate();
126 m_thread = null;
127 }
128
129 if (m_task != null)
130 {
131 m_evtCancel.Set();
132 waitForTerminate();
133 m_task = null;
134 }
135 }
136
141 protected void InternalThreadEntry(object obj)
142 {
143 m_evtRunning.Set();
145
146 try
147 {
148 if (state.InitialDelay > 0)
149 Thread.Sleep(state.InitialDelay);
150
151 if (DoWork != null)
152 DoWork(this, state);
153 }
154 finally
155 {
156 m_evtRunning.Reset();
157 m_evtDone.Set();
158 }
159 }
160
165 {
166 get
167 {
168 if (m_evtAbort.WaitOne(0))
169 return true;
170
171 return false;
172 }
173 }
174
178 public bool IsStarted
179 {
180 get
181 {
182 if (m_task == null)
183 return false;
184
185 return true;
186 }
187 }
188 }
189
194 public class ActionStateArgs<T> : EventArgs
195 {
196 CudaDnn<T> m_cuda = null;
197 Log m_log = null;
198 CancelEvent m_evtCancel;
199 int m_nDeviceID = 0;
200 int m_nInitialDelay = 0;
201 object m_arg = null;
202
212 public ActionStateArgs(CudaDnn<T> cuda, Log log, CancelEvent evtCancel, int nDeviceID = 0, object arg = null, int nInitialDelay = 0)
213 : base()
214 {
215 m_log = log;
216 m_cuda = cuda;
217 m_evtCancel = evtCancel;
218 m_nDeviceID = nDeviceID;
219 m_arg = arg;
220 m_nInitialDelay = nInitialDelay;
221 }
222
227 {
228 get { return m_cuda; }
229 set { m_cuda = value; }
230 }
231
235 public Log log
236 {
237 get { return m_log; }
238 }
239
244 {
245 get { return m_evtCancel; }
246 }
247
251 public int DeviceID
252 {
253 get { return m_nDeviceID; }
254 }
255
259 public object Arg
260 {
261 get { return m_arg; }
262 }
263
267 public int InitialDelay
268 {
269 get { return m_nInitialDelay; }
270 }
271 }
272}
The CancelEvent provides an extension to the manual cancel event that allows for overriding the manua...
Definition: CancelEvent.cs:17
void Reset()
Resets the event clearing any signaled state.
Definition: CancelEvent.cs:279
virtual void Dispose(bool disposing)
Releases all resources used by the CancelEvent.
Definition: CancelEvent.cs:318
void Set()
Sets the event to the signaled state.
Definition: CancelEvent.cs:270
The Log class provides general output in text form.
Definition: Log.cs:13
The ActionStateArgs are sent to the DoWork event when fired from the InternalThreadEntry.
object Arg
Returns the user supplied argument.
Log log
Returns the Log used for output.
int InitialDelay
Returns the initial delay in ms (if any).
CudaDnn< T > cuda
Get/set the Cuda Dnn connection to Cuda.
ActionStateArgs(CudaDnn< T > cuda, Log log, CancelEvent evtCancel, int nDeviceID=0, object arg=null, int nInitialDelay=0)
The ActionStateArgs constructor.
int DeviceID
Returns the Device ID of the device to use in the thread.
The CudaDnn object is the main interface to the Low-Level Cuda C++ DLL.
Definition: CudaDnn.cs:969
The InternalThread manages an internal thread used for Parallel and data collection operations.
void StartInternalThread(CudaDnn< T > cuda, Log log, int nDeviceID=0, object arg=null, int nInitialDelay=0)
Starts running the internal thread function which then calls the DoWork event.
void InternalThreadEntry(object obj)
Specifies the internal thread entry.
void Dispose()
Releases all resources used by the InernalThread.
bool IsStarted
Returns whether or not the internal thread has been started.
EventHandler OnPreStop
The OnPreStop event fires just after signalling the thread to stop.
bool CancellationPending
Returns whether or not a cancellation is pending.
EventHandler< ActionStateArgs< T > > DoWork
The DoWork event is the working thread function.
virtual void Dispose(bool bDisposing)
Releases all resources used by the InernalThread.
void StopInternalThread()
Stops the internal thread.
EventHandler OnPreStart
The OnPreStart event fires just before starting the thread.
InternalThread(bool bUseThreadVsTask=false)
The InternalThread constructor.
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