Thread.cpp
Go to the documentation of this file.
00001 /*******************************************************************************
00002  *  Thread.cpp
00003  *
00004  *  (C) 2006 AG Aktives Sehen <agas@uni-koblenz.de>
00005  *           Universitaet Koblenz-Landau
00006  *
00007  *  Information on Code Review state:
00008  *  §Author: R5; DevelTest: Date; Reviewer: R5; Review: Date; State: OK§
00009  *
00010  *  Additional information:
00011  *  $Id: Thread.cpp 44313 2011-04-06 22:46:28Z agas $
00012  ******************************************************************************/
00013 
00014 #include <iostream>
00015 
00016 #include "Thread.h"
00017 #include <sstream>
00018 #include "ros/ros.h"
00019 
00020 using namespace std;
00021 
00031 void threadWrapper ( Thread* instance )
00032 {
00033     // Execute thread's run code
00034     instance->run();
00035 
00036 }
00037 
00038 void Thread::start(const char* name)
00039 {
00040     // Start function as a new thread using a wrapper function
00041     int ret = pthread_create ( &m_Thread, NULL, reinterpret_cast<void* ( * ) ( void* ) > ( threadWrapper ), this );
00042     if ( ret != 0 )
00043     {
00044         ostringstream stream;
00045         stream << "pthread_create returned error " << ret;
00046         ROS_ERROR_STREAM( stream.str() );
00047     }
00048     else
00049     {
00050         if (!name) {
00051             name="UnnamedThread";
00052         }
00053         std::ostringstream s;
00054         s << "Created new Thread: \"" << name << "\" " << (int)m_Thread << " "  << std::hex << m_Thread << std::dec;
00055         ROS_INFO_STREAM( s.str() );
00056     }
00057 
00058 }
00059 
00060 void Thread::cancel()
00061 {
00062     const int max_retry = 5;
00063     int retry = 0;
00064     int ret;
00065 
00066     do
00067     {
00068         /*    cout << retry << ".." << flush;
00069             try {
00070                 ROS_INFO_STREAM( "Canceling thread..." )
00071                 sleep ( 1 );
00072                 ret = pthread_cancel ( m_Thread );
00073             } catch (...) {
00074                 ROS_INFO_STREAM( "pthread_cancel has thrown n exception" )
00075                 throw;
00076             }
00077             sleep(1);
00078             sleep ( 1 );
00079                         ret = pthread_cancel ( m_Thread );
00080             cout << ret << endl;*/
00081         ret = pthread_cancel ( m_Thread );
00082         if ( ret != 0 )
00083         {
00084             ostringstream stream;
00085             stream << "pthread_cancel returned error " << ret;
00086             ROS_ERROR_STREAM( stream.str() );
00087         }
00088         retry++;
00089         usleep ( 1000000 );
00090     }
00091     while ( ( ret != 0 ) && ( retry < max_retry ) );
00092 
00093     if (ret!=0) {
00094         ROS_ERROR( "Canceling thread failed." );
00095     } else {
00096          ROS_DEBUG_STREAM( "Thread " << m_Thread << " cancelled." );
00097     }
00098 
00099 
00100 }
00101 
00102 void Thread::join()
00103 {
00104     ROS_INFO_STREAM( "Joining Thread..." );
00105     int ret = pthread_join ( m_Thread, NULL );
00106     if ( ret != 0 )
00107     {
00108         ostringstream stream;
00109         stream << "pthread_join returned error " << ret;
00110        ROS_ERROR_STREAM( stream.str() );
00111     }
00112 }
00113 
00114 void Thread::setCancelSettings()
00115 {
00116     // Setup thread so that it can be canceled from outside
00117     pthread_setcancelstate ( PTHREAD_CANCEL_ENABLE, NULL );
00118     pthread_setcanceltype ( PTHREAD_CANCEL_DEFERRED, NULL );
00119 }
00120 
00121 void Thread::testCancel()
00122 {
00123     pthread_testcancel();
00124 }
00125 
00126 #ifdef __TEST__
00127 
00128 #include <iostream>
00129 
00130 int main ( int argc, char **argv )
00131 {
00132     std::cout << "UNITTEST" << std::endl;
00133     return 0;
00134 }
00135 #endif


robbie_architecture
Author(s): Viktor Seib
autogenerated on Mon Oct 6 2014 02:53:09