RTAB-Map 0.23.10
Real-Time Appearance-Based Mapping
Loading...
Searching...
No Matches
UThread.h
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/utilite_export.h" // DLL export/import defines
24
25#include "rtabmap/utilite/UThreadC.h"
26
86class UTILITE_EXPORT UThread : public UThreadC<void>
87{
88public:
92 enum Priority{kPLow, kPBelowNormal, kPNormal, kPAboveNormal, kPRealTime};
93
94public:
95 //return caller thread id
96 static unsigned long currentThreadId() {return (unsigned long)UThreadC<void>::Self();}
97
98public:
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
174protected:
175
176private:
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
255private:
256 void operator=(UThread &) {}
257 UThread( const UThread &) : state_(kSIdle) {}
258
259private:
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
void start()
UThread(Priority priority=kPNormal)
bool isIdle() const
bool isRunning() const
bool isKilled() const
virtual ~UThread()
void setPriority(Priority priority)
void kill()
void setAffinity(int cpu=0)
bool isCreating() const
void join(bool killFirst=false)