Main Page Class Hierarchy Alphabetical List Compound List File List Compound Members
|
00001 /******************************************************************************** 00002 * * 00003 * M u l i t h r e a d i n g S u p p o r t * 00004 * * 00005 ********************************************************************************* 00006 * Copyright (C) 2004 by Jeroen van der Zijp. All Rights Reserved. * 00007 ********************************************************************************* 00008 * This library is free software; you can redistribute it and/or * 00009 * modify it under the terms of the GNU Lesser General Public * 00010 * License as published by the Free Software Foundation; either * 00011 * version 2.1 of the License, or (at your option) any later version. * 00012 * * 00013 * This library is distributed in the hope that it will be useful, * 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00016 * Lesser General Public License for more details. * 00017 * * 00018 * You should have received a copy of the GNU Lesser General Public * 00019 * License along with this library; if not, write to the Free Software * 00020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * 00021 ********************************************************************************* 00022 * $Id: FXThread.h,v 1.16.2.1 2004/12/20 14:32:42 fox Exp $ * 00023 ********************************************************************************/ 00024 #ifndef FXTHREAD_H 00025 #define FXTHREAD_H 00026 00027 namespace FX { 00028 00029 00030 // Thread ID type 00031 #ifndef WIN32 00032 typedef unsigned long FXThreadID; 00033 #else 00034 typedef void* FXThreadID; 00035 #endif 00036 00037 00038 /** 00039 * FXMutex provides a mutex which can be used to enforce critical 00040 * sections around updates of data shared by multiple threads. 00041 */ 00042 class FXAPI FXMutex { 00043 private: 00044 unsigned long data[12]; 00045 private: 00046 FXMutex(const FXMutex&); 00047 FXMutex &operator=(const FXMutex&); 00048 public: 00049 00050 /// Initialize the mutex 00051 FXMutex(); 00052 00053 /// Lock the mutex 00054 FXbool lock(); 00055 00056 /// Try to lock the mutex; return TRUE locked 00057 FXbool trylock(); 00058 00059 /// Unlock mutex 00060 FXbool unlock(); 00061 00062 /// Delete the mutex 00063 ~FXMutex(); 00064 }; 00065 00066 00067 /** 00068 * An easy way to establish a correspondence between a C++ scope 00069 * and a critical section is to simply declare an FXMutexLock 00070 * at the beginning of the scope. 00071 * The mutex will be automatically released when the scope is 00072 * left (either by natural means or by means of an exception. 00073 */ 00074 class FXAPI FXMutexLock { 00075 private: 00076 FXMutex& mutex; 00077 private: 00078 FXMutexLock(const FXMutexLock&); 00079 FXMutexLock& operator=(const FXMutexLock&); 00080 public: 00081 FXMutexLock(FXMutex& m):mutex(m){ lock(); } 00082 FXbool lock(){ return mutex.lock(); } 00083 FXbool unlock(){ return mutex.unlock(); } 00084 ~FXMutexLock(){ unlock(); } 00085 }; 00086 00087 00088 /** 00089 * FXThread provides system-independent support for threads. 00090 * Subclasses must implement the run() function do implement 00091 * the desired functionality of the thread. 00092 * The storage of the FXThread object is to be managed by the 00093 * calling thread, not by the thread itself. 00094 */ 00095 class FXAPI FXThread { 00096 private: 00097 FXThreadID tid; 00098 #ifdef WIN32 00099 FXuint thd; 00100 #endif 00101 private: 00102 FXThread(const FXThread&); 00103 FXThread &operator=(const FXThread&); 00104 #ifdef WIN32 00105 static unsigned int CALLBACK execute(void*); 00106 #else 00107 static void* execute(void*); 00108 #endif 00109 protected: 00110 00111 /// All threads execute by deriving the run method of FXThread 00112 virtual FXint run() = 0; 00113 00114 public: 00115 00116 /// Initialize thread object. 00117 FXThread(); 00118 00119 /** 00120 * Return handle of this thread object. 00121 * This handle is valid in the context of the thread which 00122 * called start(). 00123 */ 00124 FXThreadID id() const; 00125 00126 /// Return TRUE if this thread is the calling thread. 00127 FXbool iscurrent() const; 00128 00129 /// Return TRUE if this thread is running. 00130 FXbool isrunning() const; 00131 00132 /** 00133 * Start thread; the thread is started as attached. 00134 * The thread is given stacksize for its stack; a value of 00135 * zero for stacksize will give it the default stack size. 00136 */ 00137 FXbool start(unsigned long stacksize=0); 00138 00139 /** 00140 * Suspend calling thread until thread is done. 00141 */ 00142 FXbool join(); 00143 00144 /** 00145 * Suspend calling thread until thread is done, and set code to the 00146 * return value of run() or the argument passed into exit(). 00147 * If an exception happened in the thread, return -1. 00148 */ 00149 FXbool join(FXint& code); 00150 00151 /** 00152 * Cancel the thread, stopping it immediately, running or not. 00153 * If the calling thread is this thread, nothing happens. 00154 * It is probably better to wait until it is finished, in case the 00155 * thread currently holds mutexes. 00156 */ 00157 FXbool cancel(); 00158 00159 /** 00160 * Detach thread, so that a no join() is necessary to harvest the 00161 * resources of this thread. 00162 */ 00163 FXbool detach(); 00164 00165 /** 00166 * Exit the calling thread. 00167 * No destructors are invoked for objects on thread's stack; 00168 * to invoke destructors, throw an exception instead. 00169 */ 00170 static void exit(FXint code=0); 00171 00172 /** 00173 * Return thread handle of calling thread. 00174 * The handle is valid in the context of the current thread. 00175 */ 00176 static FXThreadID current(); 00177 00178 /** 00179 * Destroy the thread immediately, running or not. 00180 * It is probably better to wait until it is finished, in case 00181 * the thread currently holds mutexes. 00182 */ 00183 virtual ~FXThread(); 00184 }; 00185 00186 } 00187 00188 #endif 00189
Copyright © 1997-2004 Jeroen van der Zijp |