Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 #include "Thread.h"
00050
00051 #if defined(__LINUX__)
00052 void* threadFunction(void* pvThread)
00053 {
00054 CThread* pclThread = (CThread*)pvThread;
00055 (pclThread->m_pfuThreadFunction)(pclThread);
00056 return NULL;
00057 }
00058 #endif
00059
00060 #if defined(__QNX__)
00061 void threadFunction(void* pvThread)
00062 {
00063 CThread* pclThread = (CThread*)pvThread;
00064 (pclThread->m_pfuThreadFunction)(pclThread);
00065 }
00066 #endif
00067
00068 #if defined(_WIN32)
00069 unsigned int __stdcall threadFunction(void* pvThread)
00070 {
00071 CThread* pclThread = (CThread*)pvThread;
00072 (pclThread->m_pfuThreadFunction)(pclThread);
00073 return 0;
00074 }
00075 #endif
00076
00077
00078
00079
00080
00081
00082
00083 CThread::CThread(void) : CMessage("CThread", g_iDebugLevel, g_bDebug, g_bDebugFile),
00084 m_uiStackSize(1228000),
00085 m_pcStack(0),
00086 m_hThreadHandle(0),
00087 m_bThreadRunFlag(false),
00088 m_bThreadStopFlag(false),
00089 m_pvThreadObject(0),
00090 m_pfuThreadFunction(0)
00091 {
00092 }
00093
00094 CThread::CThread(const CThread& clThread)
00095 {
00096 error(-1, "copy contructor : method should no be called!");
00097 }
00098
00099 CThread::~CThread(void)
00100 {
00101 debug(1, "destructed");
00102 }
00103
00104
00105
00106
00107
00108
00109
00110 CThread& CThread::operator=(const CThread& clThread)
00111 {
00112 error(-1, "assignment operator : method should not be called!");
00113 return *this;
00114 }
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128 void CThread::setThreadStackSize(unsigned int uiSize)
00129 {
00130 m_uiStackSize = uiSize;
00131 }
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145 int CThread::createThread(void (*pfuThreadFunction)(CThread*), void* pvThreadObject)
00146 {
00147 m_bThreadStopFlag = false;
00148 m_pvThreadObject = pvThreadObject;
00149 m_pfuThreadFunction = pfuThreadFunction;
00150 #if defined(_WIN32)
00151 unsigned int iThreadId;
00152
00153 m_hThreadHandle = (HANDLE)_beginthreadex(NULL, 0, threadFunction, (void*)this, 0, &iThreadId);
00154
00155 if(m_hThreadHandle == NULL)
00156 {
00157 warning("createThread : creating thread failed!");
00158 m_bThreadRunFlag = false;
00159 return -1;
00160 }
00161 else
00162 {
00163 m_bThreadRunFlag = true;
00164 return 0;
00165 }
00166 #endif
00167 #if defined(__LINUX__)
00168 pthread_attr_t Thread_attr;
00169
00170 int retVal = pthread_create(&m_hThreadHandle, NULL, threadFunction, (void*)this);
00171 if(retVal != 0)
00172 {
00173 warning("createThread : creating thread failed!");
00174 m_bThreadRunFlag = false;
00175 return -1;
00176 }
00177 else
00178 {
00179 m_bThreadRunFlag = true;
00180 return 0;
00181 }
00182 #endif
00183 #if defined(__QNX__)
00184
00185 if(m_pcStack == NULL)
00186 m_pcStack = new char[m_uiStackSize];
00187
00188 if(m_pcStack == NULL)
00189 {
00190 m_bThreadRunFlag = false;
00191 warning("createThread : creating stack failed!");
00192 return -1;
00193 }
00194 int iThreadId = _beginthread(threadFunction, m_pcStack, m_uiStackSize, (void*)this);
00195 debug(1,"CThread: create stacksize=%d\n",m_uiStackSize);
00196
00197 if( iThreadId == 0)
00198 {
00199 warning("createThread : creating thread failed!");
00200 m_bThreadRunFlag = false;
00201 delete [] m_pcStack;
00202 return -1;
00203 }
00204 else
00205 {
00206 m_bThreadRunFlag = true;
00207 return 0;
00208 }
00209
00210 #endif
00211 }
00212
00213 void CThread::exitThread()
00214 {
00215 m_bThreadRunFlag = false;
00216 #if defined(__WIN32)
00217 _endthreadex(0);
00218 #endif
00219 #if defined(__QNX__)
00220 _endthread();
00221 #endif
00222 }
00223
00224 void CThread::terminateThread()
00225 {
00226 m_bThreadStopFlag = true;
00227 }
00228
00229 bool CThread::checkThreadRun()
00230 {
00231 return m_bThreadRunFlag;
00232 }
00233
00234 bool CThread::checkThreadStop()
00235 {
00236 return m_bThreadStopFlag;
00237 }