latest version v1.9 - last update 10 Apr 2010 |
00001 /* 00002 * Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 00003 * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany 00004 * 00005 * This file is part of the LTI-Computer Vision Library (LTI-Lib) 00006 * 00007 * The LTI-Lib is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public License (LGPL) 00009 * as published by the Free Software Foundation; either version 2.1 of 00010 * the License, or (at your option) any later version. 00011 * 00012 * The LTI-Lib is distributed in the hope that it will be 00013 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty 00014 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with the LTI-Lib; see the file LICENSE. If 00019 * not, write to the Free Software Foundation, Inc., 59 Temple Place - 00020 * Suite 330, Boston, MA 02111-1307, USA. 00021 */ 00022 00023 00024 /*---------------------------------------------------------------- 00025 * project ....: LTI Digital Image/Signal Processing Library 00026 * file .......: ltiThread.h 00027 * authors ....: Thomas Rusert 00028 * organization: LTI, RWTH Aachen 00029 * creation ...: 03.11.99 00030 * revisions ..: $Id: ltiThread.h,v 1.5 2006/02/08 12:54:55 ltilib Exp $ 00031 */ 00032 00033 #ifndef _LTI_THREAD_H_ 00034 #define _LTI_THREAD_H_ 00035 00036 #include "ltiObject.h" 00037 #include "ltiMutex.h" 00038 #include "ltiSemaphore.h" 00039 00040 #ifndef _LTI_WIN32 00041 # include <pthread.h> 00042 #else 00043 # include <winbase.h> 00044 #endif 00045 00046 namespace lti { 00047 00048 /** 00049 * lti thread class. 00050 * representiation of a system thread (currently a posix thread for Unix 00051 * systems, and WIN32 thread for windows systems) 00052 * 00053 * If you need a thread, just inherit from this class and reimplement the 00054 * member run: 00055 * \code 00056 * class myThread : public thread { 00057 * protected: 00058 * void run() { 00059 * // your thread's job here! 00060 * } 00061 * }; 00062 * 00063 * ... 00064 * 00065 * // somewhere else in your code: 00066 * 00067 * myThread aThread; 00068 * aThread.start(); // do your threads job... 00069 * // continue with other things (your thread runs in parallel...) 00070 * \endcode 00071 */ 00072 class thread : public object { 00073 public: 00074 /** 00075 * default constructor 00076 */ 00077 thread(); 00078 00079 /** 00080 * destructor. 00081 * If the thread is still executing while destroying the thread object, 00082 * the thread will be forced to stop. Use join() to wait for the thread 00083 * to terminate itself. 00084 */ 00085 virtual ~thread(); 00086 00087 /** 00088 * start thread 00089 */ 00090 virtual void start(); 00091 00092 /** 00093 * forces the thread to stop executing. 00094 * overload this function to cleanup things run leaves after 00095 * cancellation. 00096 */ 00097 virtual void stop(); 00098 00099 /** 00100 * wait for thread termination 00101 */ 00102 virtual void join(); 00103 00104 /** 00105 * test if the thread is alive 00106 */ 00107 bool isAlive() const; 00108 00109 /** 00110 * returns whether the thread object represents the called thread 00111 * i.e. true if called within the run() method. 00112 */ 00113 bool representsCalledThread() const; 00114 00115 /** 00116 * returns the name of this type 00117 */ 00118 virtual const char* getTypeName() const; 00119 00120 protected: 00121 /** 00122 * method to be called when starting the thread; 00123 * contains the job the thread has to execute 00124 */ 00125 virtual void run() = 0; 00126 00127 /** 00128 * method to be called when finishing regulary or cancelling the thread; 00129 * something like a destructor for the run() method 00130 */ 00131 virtual void cleanUp() {}; 00132 00133 private: 00134 static void clean(void* threadObject); 00135 bool alive; 00136 static mutex startMutex; 00137 // semaphore used to join called thread with the calling one 00138 semaphore suspendSem; 00139 00140 # ifndef _LTI_WIN32 // UNIX/LINUX 00141 static void* execute(void* threadObject); 00142 pthread_t theThread; 00143 # else // WINDOWS 00144 static void execute(void* threadObject); 00145 // windows handle of the thread 00146 HANDLE theThread; 00147 00148 // id of the called thread 00149 unsigned int calledThreadId; 00150 00151 # endif 00152 }; 00153 00154 } 00155 00156 #endif