00001 /* BurrTools 00002 * 00003 * BurrTools is the legal property of its developers, whose 00004 * names are listed in the COPYRIGHT file, which is included 00005 * within the source distribution. 00006 * 00007 * This program is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU General Public License 00009 * as published by the Free Software Foundation; either version 2 00010 * of the License, or (at your option) any later version. 00011 00012 * This program is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 00017 * You should have received a copy of the GNU General Public License 00018 * along with this program; if not, write to the Free Software 00019 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00020 */ 00021 #ifndef __THREAD_H__ 00022 #define __THREAD_H__ 00023 00024 #ifdef WIN32 00025 #include <windows.h> 00026 #else 00027 #include <pthread.h> 00028 #endif 00029 00030 /* this class encapsulates a single thread */ 00031 class thread_c { 00032 00033 private: 00034 00035 #ifdef WIN32 00036 HANDLE id; 00037 #else 00038 pthread_t id; 00039 #endif 00040 00041 bool running; 00042 00043 public: 00044 00045 /* create the thread data structure, but don't start the thread */ 00046 thread_c(void) : running(false) {} 00047 00048 /* try to stop the thread, if that doesn't work, kill it and then 00049 * delete data structur 00050 */ 00051 virtual ~thread_c(void); 00052 00053 /* run the thread return true on success */ 00054 bool start(); 00055 00056 /* inform the thread to stop running, this is dependent on the thread */ 00057 virtual void stop() {}; 00058 00059 /* kill the thread */ 00060 void kill(); 00061 00063 bool isRunning(void) { return running; } 00064 00065 protected: 00066 00067 /* this is the function that gets started for the thread, once this 00068 * function finishes, the thread will end 00069 */ 00070 virtual void run(void) = 0; 00071 00072 #ifdef WIN32 00073 friend unsigned long __stdcall start_thread(void * dat); 00074 #else 00075 friend void * start_thread(void * dat); 00076 #endif 00077 00078 private: 00079 00080 // no copying and assigning 00081 thread_c(const thread_c&); 00082 void operator=(const thread_c&); 00083 00084 }; 00085 00086 #endif