MRXT: The Multi-Robot eXploration Tool
Multi-Robot autonomous exploration and mapping simulator.
|
00001 /* 00002 * 00003 * Author: Miguel Julia <mjulia@umh.es> 00004 * 00005 * Date: 2008 00006 * 00007 * Class ClThread 00008 * 00009 */ 00010 #pragma once 00011 #ifndef __CLASS__THREAD__ 00012 #define __CLASS__THREAD__ 00013 00014 #ifndef WIN32 00015 #include <pthread.h> 00016 #else 00017 #include <windows.h> 00018 #endif 00019 00020 #include <stdio.h> 00021 00022 /** 00023 * @brief Implements a platform-independent threaded superclass 00024 * 00025 * Derive a class from this one and implement the vitual methods: 00026 * 00027 * - setup 00028 * - onStop 00029 * - execute 00030 */ 00031 class ClThread{ 00032 00033 protected: 00034 00035 int prio; 00036 size_t stacksize; 00037 00038 public: 00039 /// Constructor 00040 ClThread(); 00041 /// Destructor 00042 virtual ~ClThread(); 00043 /// runs the setup method and starts the thread 00044 int run(); 00045 /// calls the onStop method and stops the thread 00046 void stop(); 00047 /// Prints the Thread ID 00048 void printID(char* str) { printf("THREAD ID %d: %s\n",(int)thread_id, str); }; 00049 00050 private: 00051 00052 #ifndef WIN32 00053 pthread_t m_thread; 00054 pthread_t thread_id; 00055 pthread_attr_t tattr; 00056 #else 00057 HANDLE m_thread; 00058 DWORD thread_id; 00059 #endif 00060 00061 bool running; 00062 00063 /// Thread entry point function 00064 #ifndef WIN32 00065 static void* EntryPoint(void * pthis); 00066 #else 00067 static DWORD WINAPI EntryPoint(void * pthis); 00068 #endif 00069 00070 /// this virtual method will be executed when trying to start the thead, must return 0 if ok 00071 virtual int setup(){return 0;}; 00072 00073 /// this virtual method will be executed when the stop method is called 00074 virtual void onStop(){}; 00075 00076 /// virtual method for the thread main process. 00077 virtual void execute(){}; 00078 00079 00080 }; 00081 00082 void sleepms(int milliseconds); 00083 00084 #endif 00085