AFEPack
Thread.h
浏览该文件的文档。
00001 
00011 #ifdef MULTITHREAD
00012 
00013 #ifndef __Thread_h__
00014 #define __Thread_h__
00015 
00016 #include <pthread.h>
00017 #include <iostream>
00018 #include <list>
00019 
00020 struct NullThread {};
00021 
00022 #define ARG 0
00023 #define CLASSNAME Thread0
00024 #include "ThreadBase.h"
00025 #undef CLASSNAME
00026 #undef ARG
00027 
00028 #define ARG 1
00029 #define CLASSNAME Thread1
00030 #include "ThreadBase.h"
00031 #undef CLASSNAME
00032 #undef ARG
00033 
00034 #define ARG 2
00035 #define CLASSNAME Thread2
00036 #include "ThreadBase.h"
00037 #undef CLASSNAME
00038 #undef ARG
00039 
00040 #define ARG 3
00041 #define CLASSNAME Thread3
00042 #include "ThreadBase.h"
00043 #undef CLASSNAME
00044 #undef ARG
00045 
00046 #define ARG 4
00047 #define CLASSNAME Thread4
00048 #include "ThreadBase.h"
00049 #undef CLASSNAME
00050 #undef ARG
00051 
00052 #define ARG 5
00053 #define CLASSNAME Thread5
00054 #include "ThreadBase.h"
00055 #undef CLASSNAME
00056 #undef ARG
00057 
00058 #define ARG 6
00059 #define CLASSNAME Thread6
00060 #include "ThreadBase.h"
00061 #undef CLASSNAME
00062 #undef ARG
00063 
00064 #define ARG 7
00065 #define CLASSNAME Thread7
00066 #include "ThreadBase.h"
00067 #undef CLASSNAME
00068 #undef ARG
00069 
00070 #define ARG 8
00071 #define CLASSNAME Thread8
00072 #include "ThreadBase.h"
00073 #undef CLASSNAME
00074 #undef ARG
00075 
00076 #define ARG 9
00077 #define CLASSNAME Thread9
00078 #include "ThreadBase.h"
00079 #undef CLASSNAME
00080 #undef ARG
00081 
00082 #define ARG 10
00083 #define CLASSNAME Thread10
00084 #include "ThreadBase.h"
00085 #undef CLASSNAME
00086 #undef ARG
00087 
00094 void setThread(unsigned int);
00101 unsigned int getThread();
00102 
00110 class ThreadManager
00111 {
00112  private:
00113   std::list<pthread_t> threads; 
00114   std::list<void *>    args;    
00115   bool                 is_joined; 
00116  public:
00117   ThreadManager() : 
00118     is_joined(false) {};
00119   ~ThreadManager() {
00120     if (!is_joined && !threads.empty()) {
00121       std::cerr << "Thread manager is not joined before destory."
00122                 << std::endl;
00123       abort();
00124     }
00125   };
00134   template <class T>
00135   int start(T fun_data,
00136             pthread_attr_t * attr = NULL) {
00137     pthread_t th;
00138     T * fun_data_copy = new T(fun_data); 
00142     int error_number = pthread_create(&th, 
00143                                       attr,
00144                                       fun_data.thread_entry, 
00145                                       (void *)fun_data_copy);
00146     if (error_number == 0) {
00147       threads.push_back(th);
00148       args.push_back(fun_data_copy);
00149 #ifdef DEBUG
00150       //std::cerr << "new thread created successfully. " 
00151       //        << std::endl;
00152 #endif
00153       return 0;
00154     }
00155     else {
00156       std::cout << "thread creating failure with error_number "
00157                 << error_number << std::endl;
00158       exit(-1);
00159     }
00160   };
00167   template <class T>
00168   int join(T arg_collector) {
00169     typedef typename T::FunDataType FunData;
00170     std::list<pthread_t>::iterator the_th = threads.begin();
00171     std::list<pthread_t>::iterator end_th = threads.end();
00172     std::list<void *>::iterator the_arg = args.begin();
00173     for (;the_th != end_th;the_th ++, the_arg ++) {
00174       int error_number = pthread_join(*the_th, NULL);
00175       if (error_number) {
00176         std::cout << "thread join error with error_number "
00177                   << error_number << std::endl;
00178         exit(-1);
00179       }
00180       delete (FunData *)(*the_arg);
00181     }
00182     clean();
00183     return 0;
00184   };
00185  private:
00186   void clean() {
00187     threads.clear();
00188     args.clear();
00189     is_joined = false;
00190   };
00191 };
00192 
00193 #endif // __Thread_h__
00194 
00195 #endif 
00196