UThread.h
Go to the documentation of this file.
00001 /*
00002 *  utilite is a cross-platform library with
00003 *  useful utilities for fast and small developing.
00004 *  Copyright (C) 2010  Mathieu Labbe
00005 *
00006 *  utilite is free library: you can redistribute it and/or modify
00007 *  it under the terms of the GNU Lesser General Public License as published by
00008 *  the Free Software Foundation, either version 3 of the License, or
00009 *  (at your option) any later version.
00010 *
00011 *  utilite is distributed in the hope that it will be useful,
00012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 *  GNU Lesser General Public License for more details.
00015 *
00016 *  You should have received a copy of the GNU Lesser General Public License
00017 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 #ifndef UTHREADNODE_H
00021 #define UTHREADNODE_H
00022 
00023 #include "rtabmap/utilite/UtiLiteExp.h" // DLL export/import defines
00024 
00025 #include "rtabmap/utilite/UThreadC.h"
00026 
00086 class UTILITE_EXP UThread : public UThreadC<void>
00087 {
00088 public:
00092     enum Priority{kPLow, kPBelowNormal, kPNormal, kPAboveNormal, kPRealTime};
00093 
00094 public:
00095     //return caller thread id
00096     static unsigned long currentThreadId() {return (unsigned long)UThreadC<void>::Self();}
00097 
00098 public:
00104     UThread(Priority priority = kPNormal);
00105 
00112     virtual ~UThread();
00113 
00119     void start();
00120 
00127         void kill();
00128 
00135         void join(bool killFirst = false);
00136 
00141     void setPriority(Priority priority);
00142 
00149         void setAffinity(int cpu = 0);
00150 
00154     bool isCreating() const;
00155 
00159     bool isRunning() const;
00160 
00164     bool isIdle() const;
00165 
00169     bool isKilled() const;
00170 
00171     Handle getThreadHandle() const {return handle_;}
00172     unsigned long getThreadId() const {return threadId_;}
00173 
00174 protected:
00175 
00176 private:
00183         virtual void mainLoopBegin() {}
00184 
00194         virtual void mainLoop() = 0;
00195 
00203         virtual void mainLoopKill() {}
00204 
00211         virtual void mainLoopEnd() {}
00212 
00213     /*
00214      * Inherited method ThreadMain() from Thread.
00215      * @see Thread<void>
00216      */
00217     void ThreadMain();
00218 
00219     /*
00220          * Apply thread priority. This is called when starting the thread.
00221          * *@todo : Support pthread
00222          */
00223         void applyPriority();
00224 
00225         /*
00226          * Apply cpu affinity. This is called when starting the thread.
00227          * *@todo : Support Windows
00228          */
00229         void applyAffinity();
00230 
00231     /*
00232      * Inherited method Create() from Thread.
00233      * Here we force this function to be private so the
00234      * inherited class can't have access to it.
00235      * @see Thread<void>
00236      */
00237     int Create(
00238       Handle  * const     & H               = 0,
00239       const bool          & CreateDetached  = false,
00240       const unsigned int  & StackSize       = 0,
00241       const bool          & CancelEnable    = false,   // UNUSED
00242       const bool          & CancelAsync     = false    // UNUSED
00243     ) const;
00244 
00245     //Methods from UThread<void> class hided
00246     static int Join( Handle H )
00247           { return UThreadC<void>::Join(H); }
00248 #ifndef ANDROID
00249         static int Kill( Handle H )
00250           { return UThreadC<void>::Kill(H); }
00251 #endif
00252         static int Detach( Handle H )
00253           { return UThreadC<void>::Detach(H); }
00254 
00255 private:
00256         void operator=(UThread & t) {}
00257         UThread( const UThread & t ) : state_(kSIdle) {}
00258 
00259 private:
00260     enum State{kSIdle, kSCreating, kSRunning, kSKilled}; /* Enum of states. */
00261     State state_;                       /* The thread state. */
00262     Priority priority_;         /* The thread priority. */
00263     Handle handle_;     /* The thread handle. */
00264     unsigned long threadId_; /* The thread id. */
00265     int cpuAffinity_; /* The cpu affinity. */
00266     UMutex killSafelyMutex_;    /* Mutex used to protect the kill() method. */
00267     UMutex runningMutex_;           /* Mutex used to notify the join method when the thread has finished. */
00268 };
00269 
00270 #endif // UTHREADNODE_H


rtabmap
Author(s): Mathieu Labbe
autogenerated on Sat Jul 23 2016 11:44:28