|
UniSet
2.6.0
|
00001 /* 00002 * Copyright (c) 2015 Pavel Vainerman. 00003 * 00004 * This program is free software: you can redistribute it and/or modify 00005 * it under the terms of the GNU Lesser General Public License as 00006 * published by the Free Software Foundation, version 2.1. 00007 * 00008 * This program is distributed in the hope that it will be useful, but 00009 * WITHOUT ANY WARRANTY; without even the implied warranty of 00010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 * Lesser General Lesser Public License for more details. 00012 * 00013 * You should have received a copy of the GNU Lesser General Public License 00014 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00015 */ 00016 // -------------------------------------------------------------------------- 00021 // -------------------------------------------------------------------------- 00022 #ifndef OmniThreadCreator_h_ 00023 #define OmniThreadCreator_h_ 00024 //--------------------------------------------------------------------------- 00025 #include <omnithread.h> 00026 #include <memory> 00027 //---------------------------------------------------------------------------- 00028 namespace uniset 00029 { 00089 //---------------------------------------------------------------------------------------- 00090 template<class ThreadMaster> 00091 class OmniThreadCreator: 00092 public omni_thread 00093 { 00094 public: 00095 00097 typedef void(ThreadMaster::* Action)(); 00098 00099 OmniThreadCreator( const std::shared_ptr<ThreadMaster>& m, Action a, bool undetached = false ); 00100 ~OmniThreadCreator() {} 00101 00102 inline bool isRunning() 00103 { 00104 return state() == omni_thread::STATE_RUNNING; 00105 } 00106 inline void stop() 00107 { 00108 exit(0); 00109 } 00110 inline int getTID() 00111 { 00112 return id(); 00113 } 00114 00115 inline void join() 00116 { 00117 omni_thread::join(NULL); 00118 } 00119 00120 protected: 00121 void* run_undetached(void* x) 00122 { 00123 if(m) 00124 (m.get()->*act)(); 00125 00126 return (void*)0; 00127 } 00128 00129 virtual void run(void* arg) 00130 { 00131 if(m) 00132 (m.get()->*act)(); 00133 } 00134 00135 private: 00136 OmniThreadCreator(); 00137 std::shared_ptr<ThreadMaster> m; 00138 Action act; 00139 }; 00140 00141 //---------------------------------------------------------------------------------------- 00142 template <class ThreadMaster> 00143 OmniThreadCreator<ThreadMaster>::OmniThreadCreator( const std::shared_ptr<ThreadMaster>& _m, Action a, bool undetach ): 00144 omni_thread(), 00145 m(_m), 00146 act(a) 00147 { 00148 if(undetach) 00149 start_undetached(); 00150 } 00151 //---------------------------------------------------------------------------------------- 00152 00153 template <class ThreadMaster> 00154 OmniThreadCreator<ThreadMaster>::OmniThreadCreator(): 00155 m(0), 00156 act(0) 00157 { 00158 } 00159 //---------------------------------------------------------------------------------------- 00160 } // end of uniset namespace 00161 // ------------------------------------------------------------------------- 00162 #endif // OmniThreadCreator_h_
1.7.6.1