1 /* 2 * Modification History 3 * 4 * 2000-December-13 Jason Rohrer 5 * Created. 6 * 7 * 2001-March-4 Jason Rohrer 8 * Made sleep() static so it can be called by non-Thread classes. 9 * 10 * 2001-May-12 Jason Rohrer 11 * Added comments about joining before destroying. 12 * 13 * 2002-March-29 Jason Rohrer 14 * Added Fortify inclusion. 15 * 16 * 2002-August-5 Jason Rohrer 17 * Made destructor virtual. 18 * 19 * 2004-March-31 Jason Rohrer 20 * Added support for detatched mode. 21 * 22 * 2005-January-9 Jason Rohrer 23 * Made sleep function virtual to allow overrides. 24 * 25 * 2005-January-22 Jason Rohrer 26 * Added a static sleep function. 27 */ 28 29 #include "minorGems/common.h" 30 31 32 33 #ifndef THREAD_CLASS_INCLUDED 34 #define THREAD_CLASS_INCLUDED 35 36 37 38 #ifdef FORTIFY 39 #include "minorGems/util/development/fortify/fortify.h" 40 #endif 41 42 43 44 /** 45 * Base class to be subclassed by all threads. 46 * 47 * Note: Implementation for the functions defined here is provided 48 * separately for each platform (in the mac/ linux/ and win32/ 49 * subdirectories). 50 * 51 * @author Jason Rohrer 52 */ 53 class Thread { 54 55 public: 56 57 Thread(); 58 virtual ~Thread(); 59 60 61 /** 62 * Starts this Thread. 63 * 64 * Note that after starting a non-detached thread, it _must_ be 65 * joined before being destroyed to avoid memory leaks. 66 * 67 * Threads running in detatched mode handle their own destruction 68 * as they terminate and do not need to be joined at all. 69 * 70 * @param inDetach true if this thread should run in detatched mode, 71 * or false to run in non-detached mode. Defaults to false. 72 */ 73 void start( char inDetach = false ); 74 75 76 /** 77 * To be overriden by subclasses. 78 * This method will be run by the Thread after start() has been called. 79 */ 80 virtual void run() = 0; 81 82 83 /** 84 * Blocks until this thread finishes executing its run() method. 85 * 86 * Must be called before destroying this thread, if this thread 87 * has been started. 88 */ 89 void join(); 90 91 92 /** 93 * Puts the current thread to sleep for a specified amount of time. 94 * 95 * Note that given a thread instance threadA, calling threadA.sleep() 96 * will put the calling thread to sleep. 97 * 98 * @param inTimeInMilliseconds the number of milliseconds to sleep. 99 */ sleep(unsigned long inTimeInMilliseconds)100 virtual void sleep( unsigned long inTimeInMilliseconds ) { 101 staticSleep( inTimeInMilliseconds ); 102 } 103 104 105 106 /** 107 * Same as sleep, but can be called without constructing a thread. 108 */ 109 static void staticSleep( unsigned long inTimeInMilliseconds ); 110 111 112 113 /** 114 * Gets whether this thread is detached. 115 * 116 * @return true if this thread is detached. 117 */ isDetatched()118 char isDetatched() { 119 return mIsDetached; 120 } 121 122 123 124 private: 125 126 /** 127 * Used by platform-specific implementations. 128 */ 129 void *mNativeObjectPointer; 130 131 132 char mIsDetached; 133 134 }; 135 136 #endif 137