UniSet  1.4.0
ThreadCreator.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 ThreadCreator_h_
00026 #define ThreadCreator_h_
00027 //----------------------------------------------------------------------------------------
00028 #include "PosixThread.h"
00029 //----------------------------------------------------------------------------------------
00083 //----------------------------------------------------------------------------------------
00084 template<class ThreadMaster>
00085 class ThreadCreator:
00086     protected PosixThread
00087 {
00088     public:
00089     
00091         typedef void(ThreadMaster::* Action)(void); 
00092 
00093         pthread_t start();
00094         pthread_t getTID(){ return PosixThread::getTID(); }
00095 
00096         inline void stop()
00097         {
00098             PosixThread::stop();
00099         }
00100         
00101         inline void kill( int signo )       
00102         {
00103             PosixThread::thrkill(signo);
00104         }
00105         
00106         inline void setPriority( int priority )
00107         {
00108             PosixThread::setPriority(priority);
00109         }
00110 
00111         ThreadCreator( ThreadMaster* m, Action a );
00112         ~ThreadCreator();
00113 
00114     protected:
00115         virtual void work(); 
00117     private:
00118         ThreadCreator();
00119     
00120         ThreadMaster* m;
00121         Action act;
00122 
00123 };
00124 
00125 //----------------------------------------------------------------------------------------
00126 template <class ThreadMaster>
00127 ThreadCreator<ThreadMaster>::ThreadCreator( ThreadMaster* m, Action a ):
00128     m(m),
00129     act(a)
00130 {
00131 }
00132 //----------------------------------------------------------------------------------------
00133 template <class ThreadMaster>
00134 void ThreadCreator<ThreadMaster>::work()
00135 {
00136     if(m)
00137         (m->*act)();
00138 //  PosixThread::stop()
00139 }
00140 //----------------------------------------------------------------------------------------
00141 template <class ThreadMaster>
00142 pthread_t ThreadCreator<ThreadMaster>::start()
00143 {
00144     PosixThread::start( static_cast<PosixThread*>(this) );
00145     return getTID();
00146 }
00147 
00148 //----------------------------------------------------------------------------------------
00149 template <class ThreadMaster>
00150 ThreadCreator<ThreadMaster>::ThreadCreator():
00151     m(0),
00152     act(0)
00153 {
00154 }
00155 //----------------------------------------------------------------------------------------
00156 template <class ThreadMaster>
00157 ThreadCreator<ThreadMaster>::~ThreadCreator()
00158 {
00159 }
00160 //----------------------------------------------------------------------------------------
00161 #endif // ThreadCreator_h_