Main Page | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages

thread.h

Go to the documentation of this file.
00001 
00025 #ifndef __THREAD_H__
00026 #define __THREAD_H__
00027 
00028 
00029 #include <pthread.h>
00030 #include <unistd.h>
00031 
00033 enum tTheadState
00034 {
00035   eTsUnknown,
00036   eTsSuspend,
00037   eTsRun,
00038   eTsExit
00039 };
00040 
00041 
00047 class cThread
00048 {
00049 protected:
00051   pthread_t   m_thread;
00052   bool        m_main;  
00053 
00054   tTheadState m_state;
00055 
00056   static void *Thread( void *param );
00057 
00058 public:
00059   cThread();
00060   cThread( const pthread_t &thread, bool main_thread, tTheadState state );
00061   virtual ~cThread();
00062 
00063   // get the current thread class
00064   static cThread *GetThread();
00065 
00066   // start thread
00067   virtual bool Start();
00068 
00069   // wait for thread termination
00070   virtual bool Wait( void *&rv );
00071 
00073   bool IsRunning() { return m_state == eTsRun; }
00075   bool IsMain()  { return m_main; }
00076 
00077 protected:
00079   virtual void *Run() = 0;
00080   virtual void Exit( void *rv );
00081 };
00082 
00083 
00089 class cThreadLock
00090 {
00091 protected:
00093   pthread_mutex_t m_lock;
00094 
00095 public:
00096   cThreadLock();
00097   virtual ~cThreadLock();
00098 
00099   virtual void Lock();
00100   virtual void Unlock();
00101 
00102   virtual bool TryLock();
00103 };
00104 
00111 class cThreadLockAuto
00112 {
00114   cThreadLock &m_lock;
00115 
00116 public:
00118   cThreadLockAuto( cThreadLock &lock )
00119     : m_lock( lock )
00120   {
00121     m_lock.Lock();
00122   }
00124   ~cThreadLockAuto()
00125   {
00126     m_lock.Unlock();
00127   }
00128 };
00129 
00130 
00136 class cThreadLockRw
00137 {
00138 protected:
00140   pthread_rwlock_t m_rwlock;
00141 
00142 public:
00143   cThreadLockRw();
00144   virtual ~cThreadLockRw();
00145 
00146   virtual void ReadLock();
00147   virtual void ReadUnlock();
00148   virtual bool TryReadLock();
00149 
00150   virtual void WriteLock();
00151   virtual void WriteUnlock();
00152   virtual bool TryWriteLock();
00153 
00154   // true => no lock held
00155   bool CheckLock();
00156 };
00157 
00163 class cThreadCond : public cThreadLock
00164 {
00165 protected:
00167   pthread_cond_t m_cond;
00168 
00169 public:
00170   cThreadCond();
00171   virtual ~cThreadCond();
00172 
00173   // call Lock before Signal
00174   virtual void Signal();
00175 
00176   // call Lock before Wait
00177   virtual void Wait();
00178 };
00179 
00180 
00181 #endif

Generated on Mon Apr 26 14:36:48 2010 for New Simulator by  doxygen 1.4.4