Changeset 35:f8a5b321d0c3 in xplcommon for src
- Timestamp:
- 01/03/13 06:57:15 (12 years ago)
- Branch:
- default
- Phase:
- public
- Location:
- src/xplcommon
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
src/xplcommon/Thread.h
r20 r35 53 53 54 54 public: 55 /** 56 * Construct the thread. 57 * 58 * @param detached if true, the thread will be created as a 59 * detached one, i.e. it can't be joined later on. The thread 60 * object will also be deleted when its run() function has 61 * finished. 62 */ 63 Thread(bool detached = false); 64 55 65 /** 56 66 * Virtual destructor. -
src/xplcommon/posix/Thread.cc
r3 r35 40 40 void* Thread::threadFn(void* arg) 41 41 { 42 reinterpret_cast<Thread*>(arg)->run(); 42 Thread* thread = reinterpret_cast<Thread*>(arg); 43 thread->run(); 44 if (thread->detached) delete thread; 43 45 return 0; 46 } 47 48 //------------------------------------------------------------------------------ 49 50 bool Thread::start() 51 { 52 pthread_attr_t attr; 53 pthread_attr_init(&attr); 54 pthread_attr_setdetachstate(&attr, detached ? PTHREAD_CREATE_DETACHED : 55 PTHREAD_CREATE_JOINABLE); 56 bool isOK = pthread_create(&thread, &attr, &threadFn, this)>=0; 57 pthread_attr_destroy(&attr); 58 return isOK; 44 59 } 45 60 -
src/xplcommon/posix/Thread.h
r4 r35 59 59 60 60 /** 61 * Indicate if the thread is detached or not. 62 */ 63 bool detached; 64 65 /** 61 66 * The posix thread handle. 62 67 */ … … 64 69 65 70 public: 71 /** 72 * Construct the thread 73 */ 74 Thread(bool detached = false); 75 66 76 /** 67 77 * Virtual destructor. … … 98 108 //------------------------------------------------------------------------------ 99 109 100 inline Thread::~Thread() 110 inline Thread::Thread(bool detached) : 111 detached(detached) 101 112 { 102 113 } … … 104 115 //------------------------------------------------------------------------------ 105 116 106 inline bool Thread::start()117 inline Thread::~Thread() 107 118 { 108 return pthread_create(&thread, 0, &threadFn, this)>=0;109 119 } 110 120 -
src/xplcommon/win32/Thread.cc
r3 r35 40 40 DWORD Thread::threadFn(LPVOID arg) 41 41 { 42 reinterpret_cast<Thread*>(arg)->run(); 42 Thread* thread = reinterpret_cast<Thread*>(arg); 43 44 thread->run(); 45 46 if (thread->detached) delete thread; 47 43 48 return 0; 44 49 } -
src/xplcommon/win32/Thread.h
r20 r35 58 58 59 59 /** 60 * Indicate if the thread is detached. 61 */ 62 bool detached; 63 64 /** 60 65 * The thread handle. 61 66 */ … … 63 68 64 69 public: 70 /** 71 * Construct the thread object. 72 */ 73 Thread(bool detached = false); 74 65 75 /** 66 76 * Virtual destructor. … … 84 94 */ 85 95 void join(); 96 97 void release(); 86 98 }; 87 99 … … 97 109 //------------------------------------------------------------------------------ 98 110 111 inline Thread::Thread(bool detached) : 112 detached(detached), 113 handle(0) 114 { 115 } 116 117 //------------------------------------------------------------------------------ 118 99 119 inline Thread::~Thread() 100 120 { … … 106 126 { 107 127 handle = CreateThread(0, 0, &threadFn, this, 0, 0); 108 return handle!=0; 128 bool isOK = handle!=0; 129 if (detached && handle!=0) { 130 CloseHandle(handle); 131 handle = 0; 132 } 133 return isOK; 109 134 } 110 135 … … 113 138 inline void Thread::join() 114 139 { 115 WaitForSingleObject(handle, INFINITE); 140 if (handle!=0) { 141 WaitForSingleObject(handle, INFINITE); 142 CloseHandle(handle); 143 } 116 144 } 117 145
Note:
See TracChangeset
for help on using the changeset viewer.