MRXT: The Multi-Robot eXploration Tool
Multi-Robot autonomous exploration and mapping simulator.
src/mylib/ClThread.cpp
00001 
00002 #include "ClThread.h"
00003 #ifndef WIN32
00004 #include <pthread.h>
00005 #include <signal.h>
00006 #include <unistd.h>
00007 #include <sys/time.h> // getrlimit()
00008 #include <sys/resource.h> // getrlimit()
00009 #else
00010 #include <windows.h>
00011 #endif
00012 #include <stdio.h>
00013 
00014 
00015 ClThread::ClThread():
00016         stacksize(0),
00017         running(false)
00018 {
00019         //printf("[CLTHREAD] Th class created \n");
00020 }
00021 
00022 ClThread::~ClThread(){
00023         stop();
00024         //printf("[CLTHREAD] Th class destroyed \n");
00025 }
00026 
00027 int ClThread::run(){
00028 #ifndef WIN32
00029         int ret;
00030         sched_param param;
00031 #endif
00032 
00033         if(!setup()){ 
00034                 
00035                 #ifndef WIN32
00036                 sigset_t mask, oldmask;
00037                 sigemptyset (&mask);
00038                 sigaddset (&mask, SIGUSR1);
00039                 pthread_sigmask(SIG_BLOCK, &mask, &oldmask);
00040 
00041                 /* initialized with default attributes */
00042                 ret = pthread_attr_init (&tattr);
00043 
00044                 /* safe to get existing scheduling param */
00045                 ret = pthread_attr_getschedparam (&tattr, &param);
00046 
00047                 /* set the priority; others are unchanged */
00048                 //param.sched_priority = prio;
00049 
00050                 /* setting the new scheduling param */
00051                 ret = pthread_attr_setschedparam (&tattr, &param);
00052 
00053                 if (stacksize) pthread_attr_setstacksize (&tattr, stacksize);
00054                 
00055                 pthread_create(&thread_id, &tattr, ClThread::EntryPoint, (void*) this);
00056 
00057                 #else
00058                 DWORD m_stackSize = 0;
00059                 m_thread = CreateThread(NULL, m_stackSize, ClThread::EntryPoint, (LPVOID) this, 0, &thread_id);
00060                 #endif
00061                 running = true;
00062                 return(0);
00063         }
00064         else 
00065                 return(-1);
00066 }
00067 
00068 void ClThread::stop()
00069 // This method closes the connection
00070 {
00071         if(running){
00072                 running = false;
00073                 onStop();
00074                 #ifndef WIN32
00075                 pthread_cancel(thread_id);                              // kill thread
00076                 pthread_detach(thread_id);      
00077                 #else
00078                 //TerminateThread(m_thread,0);
00079                 CloseHandle( m_thread );
00080                 #endif
00081         }
00082 }
00083 
00084 #ifndef WIN32 
00085 void *ClThread::EntryPoint(void * pthis)
00086 #else
00087 DWORD WINAPI ClThread::EntryPoint(void * pthis)
00088 #endif 
00089 // Thread entry point. The thread start function must be static. In order to uses non 
00090 // static methods and properties we make use of this function.
00091 // This static function calls the non static method execute() receiving the whole object as a
00092 // parameter
00093 //
00094 {
00095         ClThread *th = (ClThread*)pthis;        // cast the object
00096         th->execute();
00097 #ifndef WIN32
00098         pthread_exit(NULL);
00099 #else
00100         ExitThread(NULL);
00101 #endif
00102         return 0;
00103 }
00104 
00105 void sleepms(int milliseconds){
00106 #ifdef WIN32
00107         Sleep(milliseconds);
00108 #else
00109         usleep(1000*milliseconds);
00110 #endif
00111 
00112 }
 All Classes Functions Variables Typedefs