UniSet  2.2.1
OmniThreadCreator.h
См. документацию.
00001 /* This file is part of the UniSet project
00002  * Copyright (c) 2002 Free Software Foundation, Inc.
00003  * Copyright (c) 2002 Pavel Vainerman
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018  */
00019 // --------------------------------------------------------------------------
00024 // --------------------------------------------------------------------------
00025 #ifndef OmniThreadCreator_h_
00026 #define OmniThreadCreator_h_
00027 //---------------------------------------------------------------------------
00028 #include <omnithread.h>
00029 #include <memory>
00030 //----------------------------------------------------------------------------
00090 //----------------------------------------------------------------------------------------
00091 template<class ThreadMaster>
00092 class OmniThreadCreator:
00093     public omni_thread
00094 {
00095     public:
00096 
00098         typedef void(ThreadMaster::* Action)();
00099 
00100         OmniThreadCreator( const std::shared_ptr<ThreadMaster>& m, Action a, bool undetached = false );
00101         ~OmniThreadCreator() {}
00102 
00103         inline bool isRunning()
00104         {
00105             return state() == omni_thread::STATE_RUNNING;
00106         }
00107         inline void stop()
00108         {
00109             exit(0);
00110         }
00111         inline pid_t getTID()
00112         {
00113             return id();
00114         }
00115 
00116         inline void join()
00117         {
00118             omni_thread::join(NULL);
00119         }
00120 
00121     protected:
00122         void* run_undetached(void* x)
00123         {
00124             if(m)
00125                 (m.get()->*act)();
00126 
00127             return (void*)0;
00128         }
00129 
00130         virtual void run(void* arg)
00131         {
00132             if(m)
00133                 (m.get()->*act)();
00134         }
00135 
00136     private:
00137         OmniThreadCreator();
00138         std::shared_ptr<ThreadMaster> m;
00139         Action act;
00140 };
00141 
00142 //----------------------------------------------------------------------------------------
00143 template <class ThreadMaster>
00144 OmniThreadCreator<ThreadMaster>::OmniThreadCreator( const std::shared_ptr<ThreadMaster>& _m, Action a, bool undetach  ):
00145     omni_thread(),
00146     m(_m),
00147     act(a)
00148 {
00149     if(undetach)
00150         start_undetached();
00151 }
00152 //----------------------------------------------------------------------------------------
00153 
00154 template <class ThreadMaster>
00155 OmniThreadCreator<ThreadMaster>::OmniThreadCreator():
00156     m(0),
00157     act(0)
00158 {
00159 }
00160 //----------------------------------------------------------------------------------------
00161 
00162 #endif // OmniThreadCreator_h_