Thread.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by Pierre Schmitt, Ludovic Giacomello, Nhi Ly   *
00003  *      *
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                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 
00022 #ifndef THREAD_H
00023 #define THREAD_H
00024 
00025 #include<string>
00026 #include<iostream>
00027 
00028 #include<unistd.h>
00029 #include<pthread.h>
00030 
00031 using namespace std;
00032 
00037 class Thread {
00038 public:
00043         Thread() { mExecution=false; }
00044 
00050         Thread( const Thread &t ){
00051                 mExecution=t.mExecution;
00052                 mThread=t.mThread;
00053         }
00058         virtual ~Thread(){
00059                 if(mExecution) pthread_detach( mThread );
00060         }
00061 
00066         void Demarrer(){
00067                 if(!mExecution){
00068                         int v=pthread_create(&mThread,0,Filament,this);
00069                         if(v!=0 ) cerr<<endl<<"Erreur avec pthread_create()!"<<endl;
00070                 }
00071         }
00072 
00076         void Arreter(){
00077                 if(mExecution){
00078                         pthread_cancel(mThread);
00079                         mExecution=false;
00080                 }
00081         }
00082 
00086         void AttendreFin(){
00087                 if(mExecution) {
00088                         if(pthread_join(mThread,NULL)!=0 ) cerr<<endl<<"Erreur avec pthread_join()!"<<endl;
00089                         mExecution = false;
00090                 }
00091         }
00092 
00096         virtual void* Traitement()=0;
00097 private:                
00103         static void *Filament(void *pthis){
00104                 try{                    
00105                         Thread *parent = (Thread*)pthis;
00106                         parent->mExecution=true;
00107                         parent->Traitement();
00108                 }
00109                 catch(...){ cerr<<"Filament est partie en sucette!!!"<<endl; }
00110                 pthread_exit(0);
00111         }
00112 
00116         bool mExecution;
00117 
00121         pthread_t mThread;
00122 };
00123 
00124 #endif

Generated on Wed Jan 2 14:01:41 2008 for Pacman by  doxygen 1.5.1