00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef _METRIVIS_THREAD_H
00023 #define _METRIVIS_THREAD_H
00024
00032 #include <boost/thread/thread.hpp>
00033 #include <boost/thread/mutex.hpp>
00034 #include <string>
00035
00036
00037 namespace metrivis {
00038
00039
00086 class Thread {
00087
00088 public:
00092 Thread();
00093
00097 virtual ~Thread();
00098
00107 virtual int Init();
00108
00115 int Start();
00116
00124 int Stop();
00125
00133 int StopBlocking();
00134
00143 virtual int Uninit();
00144
00145
00151 int RunBlocking();
00152
00153
00159 void Wait();
00160
00165 inline boost::thread& thread() const {
00166 return *thread_;
00167 }
00168
00173 inline bool IsRunning() const {
00174 return (internal_state_ == Thread::RUNNING);
00175 }
00176
00181 inline void set_name(const std::string& name) {
00182 name_ = name;
00183 }
00184
00189 inline const std::string& name() const {
00190 return name_;
00191 }
00192
00193
00194 protected:
00195
00210 virtual void Run() = 0;
00211
00212
00213 private:
00214
00220 void InternalRun();
00221
00222
00223 protected:
00224
00225 volatile bool running_;
00226
00227
00228 private:
00229
00236 enum State {
00237 UNUSED = 0,
00238 INITIALIZED,
00239 RUNNING
00240 };
00241 State internal_state_;
00242
00243 boost::thread* thread_;
00244 boost::mutex run_mutex_;
00245
00246 std::string name_;
00247 };
00248
00249 }
00250
00251 #endif //_METRIVIS_THREAD_H