Changeset 8830 for trunk/MagicSoft/Cosy/base/MThread.cc
- Timestamp:
- 01/24/08 11:49:25 (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/MagicSoft/Cosy/base/MThread.cc
r8376 r8830 1 1 #include <MThread.h> 2 3 #include <iostream>4 5 #include <pthread.h>6 #include <sys/resource.h> // PRIO_PROCESS7 8 #undef DEBUG9 //#define DEBUG10 2 11 3 using namespace std; 12 4 13 // ---------------------------------------------------------------------- 14 // 15 // Constructor 16 // 17 // Starts the derived thread if you don't specify false. 18 // 19 MThread::MThread(bool start, int prio) : fIsRunning(false), fIsDetached(false), fThread(NULL), fReturn(NULL) 5 TString MyThreadX::GetThreadStateStr() const 20 6 { 21 #ifdef DEBUG 22 cout << "MThread::MThread" << endl; 23 #endif 24 if (!start) 25 return; 26 27 SetPriority(prio); 28 Start(); 7 switch (fThread.GetState()) 8 { 9 case TThread::kInvalidState: 10 return "Invalid - thread was not created properly"; 11 case TThread::kNewState: 12 return "New - thread object exists but hasn't started"; 13 case TThread::kRunningState: 14 return "Running - thread is running"; 15 case TThread::kTerminatedState: 16 return "Terminated - thread has terminated but storage has not yet been reclaimed (i.e. waiting to be joined)"; 17 case TThread::kFinishedState: 18 return "Finished - thread has finished"; 19 case TThread::kCancelingState: 20 return "Canceling - thread in process of canceling"; 21 case TThread::kCanceledState: 22 return "Canceled - thread has been canceled"; 23 case TThread::kDeletingState: 24 return "Deleting - thread in process of deleting"; 25 }; 26 return "Unknown"; 29 27 } 30 31 // ----------------------------------------------------------------------32 //33 // Destructor34 //35 // Stops the derived thread if it is still running.36 //37 MThread::~MThread()38 {39 #ifdef DEBUG40 cout << "~MThread::MThread" << endl;41 #endif42 Stop();43 }44 45 // ----------------------------------------------------------------------46 //47 // Detach the derived thread. This means, that if the thread ends the48 // needed resources (eg. for storing the return code) are freed.49 // In other word you cannot get any value back from the Thread.50 //51 void MThread::Detach()52 {53 if (fIsRunning)54 pthread_detach(*fThread);55 56 fIsDetached = true;57 }58 59 // ----------------------------------------------------------------------60 //61 // Sets the priority of the thread.62 // -20 highest priority63 // 0 standard64 // +20 lowest priority65 // This can only be done before the thread is started.66 //67 bool MThread::SetPriority(int prio)68 {69 if (fIsRunning)70 return false;71 72 fPriority = prio;73 return true;74 }75 76 // ----------------------------------------------------------------------77 //78 // Now we are back in a class instance, but running in new thread.79 // All class members can be accessed like before.80 // Set the flag for a running thread. Check if the thread should get81 // detached. Set the priority of the thread. Now reset the stop flag and82 // execute the thread. After the thread stopped it's execution reset the83 // running flag and exit.84 //85 void *MThread::RunThread()86 {87 fIsRunning = true;88 89 if (fIsDetached)90 pthread_detach(pthread_self());91 92 setpriority(PRIO_PROCESS, 0, fPriority); //lowest priority93 94 fStop = false;95 96 #ifdef DEBUG97 cout << "MThread::RunThread" << endl;98 #endif99 100 void *rc = Thread();101 102 #ifdef DEBUG103 cout << "MThread::RunThread...done." << endl;104 #endif105 106 fIsRunning = false;107 108 return rc;109 }110 111 // ----------------------------------------------------------------------112 //113 // Get the Instance back from the thread argument and call the114 // RunThread member function, which handles all MThread bspecific stuff.115 //116 void *MThread::MapThread(void *arg)117 {118 MThread *thread = (MThread*)arg;119 #ifdef DEBUG120 cout << "MThread::MapThread" << endl;121 #endif122 return thread->RunThread();123 }124 125 // ----------------------------------------------------------------------126 //127 // A thread is created and started (MapThread).128 // As an argument the actual instance is used.129 //130 void MThread::Start()131 {132 fThread = new pthread_t;133 pthread_create(fThread, NULL, MapThread, this);134 }135 136 // ----------------------------------------------------------------------137 //138 // Check if a thread is existing and running.139 // If the thread is detached, cancel the thread. Otherwise set the stop140 // flag and wait for the thread to exit.141 //142 void MThread::Stop()143 {144 #ifdef DEBUG145 cout << "MThread::Stop: fThread=" << fThread << ", fIsRunning=" << (int)fIsRunning << endl;146 #endif147 148 if (!fThread || !fIsRunning)149 return;150 151 if (fIsDetached)152 {153 #ifdef DEBUG154 cout << "Stopping detached thread..." << flush;155 #endif156 pthread_cancel(*fThread);157 fIsRunning = false;158 }159 else160 {161 #ifdef DEBUG162 cout << "Stopping thread..." << flush;163 #endif164 fStop = true;165 pthread_join(*fThread, &fReturn);166 }167 #ifdef DEBUG168 cout << "done." << endl;169 #endif170 171 delete fThread;172 fThread = NULL;173 174 #ifdef DEBUG175 cout << "MThread::Stop() done." << endl;176 #endif177 }
Note:
See TracChangeset
for help on using the changeset viewer.