UThread.h
Go to the documentation of this file.
1 /*
2 * utilite is a cross-platform library with
3 * useful utilities for fast and small developing.
4 * Copyright (C) 2010 Mathieu Labbe
5 *
6 * utilite is free library: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * utilite is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef UTHREADNODE_H
21 #define UTHREADNODE_H
22 
23 #include "rtabmap/utilite/UtiLiteExp.h" // DLL export/import defines
24 
26 
86 class UTILITE_EXP UThread : public UThreadC<void>
87 {
88 public:
92  enum Priority{kPLow, kPBelowNormal, kPNormal, kPAboveNormal, kPRealTime};
93 
94 public:
95  //return caller thread id
96  static unsigned long currentThreadId() {return (unsigned long)UThreadC<void>::Self();}
97 
98 public:
104  UThread(Priority priority = kPNormal);
105 
112  virtual ~UThread();
113 
119  void start();
120 
127  void kill();
128 
135  void join(bool killFirst = false);
136 
141  void setPriority(Priority priority);
142 
149  void setAffinity(int cpu = 0);
150 
154  bool isCreating() const;
155 
159  bool isRunning() const;
160 
164  bool isIdle() const;
165 
169  bool isKilled() const;
170 
171  Handle getThreadHandle() const {return handle_;}
172  unsigned long getThreadId() const {return threadId_;}
173 
174 protected:
175 
176 private:
183  virtual void mainLoopBegin() {}
184 
194  virtual void mainLoop() = 0;
195 
203  virtual void mainLoopKill() {}
204 
211  virtual void mainLoopEnd() {}
212 
213  /*
214  * Inherited method ThreadMain() from Thread.
215  * @see Thread<void>
216  */
217  void ThreadMain();
218 
219  /*
220  * Apply thread priority. This is called when starting the thread.
221  * *@todo : Support pthread
222  */
223  void applyPriority();
224 
225  /*
226  * Apply cpu affinity. This is called when starting the thread.
227  * *@todo : Support Windows
228  */
229  void applyAffinity();
230 
231  /*
232  * Inherited method Create() from Thread.
233  * Here we force this function to be private so the
234  * inherited class can't have access to it.
235  * @see Thread<void>
236  */
237  int Create(
238  Handle * const & H = 0,
239  const bool & CreateDetached = false,
240  const unsigned int & StackSize = 0,
241  const bool & CancelEnable = false, // UNUSED
242  const bool & CancelAsync = false // UNUSED
243  ) const;
244 
245  //Methods from UThread<void> class hided
246  static int Join( Handle H )
247  { return UThreadC<void>::Join(H); }
248 #ifndef ANDROID
249  static int Kill( Handle H )
250  { return UThreadC<void>::Kill(H); }
251 #endif
252  static int Detach( Handle H )
253  { return UThreadC<void>::Detach(H); }
254 
255 private:
256  void operator=(UThread &) {}
257  UThread( const UThread &) : state_(kSIdle) {}
258 
259 private:
260  enum State{kSIdle, kSCreating, kSRunning, kSKilled}; /* Enum of states. */
261  State state_; /* The thread state. */
262  Priority priority_; /* The thread priority. */
263  Handle handle_; /* The thread handle. */
264  unsigned long threadId_; /* The thread id. */
265  int cpuAffinity_; /* The cpu affinity. */
266  UMutex killSafelyMutex_; /* Mutex used to protect the kill() method. */
267  UMutex runningMutex_; /* Mutex used to notify the join method when the thread has finished. */
268 };
269 
270 #endif // UTHREADNODE_H
static int Detach(Handle H)
#define UTILITE_EXP
Definition: UtiLiteExp.h:33
static int Create(const Handler &Function, Thread_C_R Param, Handle *const &H=0, const bool &CreateDetached=false, const unsigned int &StackSize=0, const bool &CancelEnable=false, const bool &CancelAsync=false)
int cpuAffinity_
Definition: UThread.h:265
Priority priority_
Definition: UThread.h:262
static int Detach(Handle H)
Definition: UThread.h:252
static int Join(Handle H)
virtual void ThreadMain(Thread_R)=0
UMutex runningMutex_
Definition: UThread.h:267
State state_
Definition: UThread.h:261
UMutex killSafelyMutex_
Definition: UThread.h:266
static int Kill(Handle H)
static int Kill(Handle H)
Definition: UThread.h:249
UThread(const UThread &)
Definition: UThread.h:257
Definition: UMutex.h:54
virtual void mainLoopBegin()
Definition: UThread.h:183
virtual void mainLoopKill()
Definition: UThread.h:203
static int Join(Handle H)
Definition: UThread.h:246
virtual void mainLoopEnd()
Definition: UThread.h:211
Priority
Definition: UThread.h:92
static void applyAffinity(Mem *pRec, char affinity, u8 enc)
Definition: sqlite3.c:66304
Handle getThreadHandle() const
Definition: UThread.h:171
Handle handle_
Definition: UThread.h:263
unsigned long getThreadId() const
Definition: UThread.h:172
unsigned long threadId_
Definition: UThread.h:264
THREAD_HANDLE Handle
void operator=(UThread &)
Definition: UThread.h:256
static Handle Self()
static unsigned long currentThreadId()
Definition: UThread.h:96


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Jan 23 2023 03:38:58