00001 // ************************************************************** 00002 // Com ++ Framework by the C++ World 00003 // Please report defects: mailto:compp@cplusplusworld.com 00004 // Please visit: http://www.cplusplusworld.com/socketlibrary.html 00005 // ************************************************************** 00006 00007 #include <iostream> 00008 00009 #include "SysPlusPlus/syscall.h" 00010 #include "ComPlusPlus/Thread.h" 00011 #include "SysPlusPlus/ComException.h" 00012 #include <signal.h> 00013 00014 00020 void compp::Thread::InitPThr() { 00021 00022 PThr_id = 0; 00023 00024 PAttr = NULL; 00025 00026 Arguments = NULL; 00027 } 00028 00029 00030 00034 compp::Thread::Thread() { 00035 00036 InitPThr(); 00037 00038 } 00039 00040 00044 compp::Thread::~Thread() { 00045 } 00046 00050 compp::Thread::Thread(void *arg) { 00051 00052 InitPThr(); 00053 00054 Start ( arg ); 00055 00056 } 00057 00058 00062 static void *sfunc (void* v ) { 00063 00064 compp::Thread * f = (compp::Thread *) v; 00065 00066 f->StartThreadFunc(); 00067 return NULL; 00068 } 00069 00070 00074 void * compp::Thread::StartThreadFunc( ) { 00075 00076 Run ( Arguments ); 00077 return NULL; 00078 } 00079 00084 bool compp::Thread::Start ( void *arg ) { 00085 00086 Arguments = arg; 00087 int i; 00088 i = syspp::Call::Pthread_create ( &PThr_id, NULL, sfunc, this ) ; 00089 00090 if ( i != 0 ) { 00091 std::cout << "Thread cannot be created.\n"; 00092 throw syspp::ComException ( "Cannot create thread." ); 00093 } 00094 00095 return true; 00096 } 00097 00098 00104 bool compp::Thread::Join ( void **value_ptr ) { 00105 int i; 00106 00107 i = syspp::Call::Pthread_join ( PThr_id, value_ptr ); 00108 return i ? false : true; 00109 00110 } 00116 bool compp::Thread::Detach ( ) { 00117 int i; 00118 00119 i = syspp::Call::Pthread_detach ( PThr_id ); 00120 return i ? false : true; 00121 00122 } 00123 00127 bool compp::Thread::Terminate ( ) { 00128 int j=0; 00129 00130 pthread_exit ( &j ); 00131 return true; 00132 } 00133 00134
1.6.3