36 : m_finalizeOnStop(
false)
37 , m_handle(
new Handle()) { }
44 Thread(
const Thread& _thread)
46 this->m_handle =
new Handle();
47 this->m_finalizeOnStop = _thread.m_finalizeOnStop;
50 Thread& operator=(
const Thread& _thread)
54 delete this->m_handle;
55 this->m_handle =
new Handle();
56 this->m_finalizeOnStop = _thread.m_finalizeOnStop;
61 thread_t* native_handle()
63 return &(m_handle->nativeHandle);
66 bool start_thread(
bool wait =
true)
69 if (!m_handle->running)
71 m_handle->notifiedStop =
false;
72 if (thread_create(&(m_handle->nativeHandle), Thread::ThreadHandler, ((
void*)
static_cast<Thread*
>(
this))))
75 m_handle->condition.wait(m_handle->mutex, m_handle->running);
82 void stop_thread(
bool wait =
true)
87 m_handle->notifiedStop =
true;
88 m_handle->condition.notify_all();
94 m_handle->condition.wait(m_handle->mutex, m_handle->stopped);
98 bool wait_thread(
unsigned millisec)
101 return m_handle->stopped ? true : m_handle->condition.wait_for(m_handle->mutex, millisec, m_handle->stopped);
107 return m_handle->running;
113 return m_handle->notifiedStop || m_handle->stopped;
116 void pause(
unsigned millisec)
120 while (!m_handle->notifiedStop && !m_handle->notifiedWake && m_handle->condition.wait_for(m_handle->mutex, _timeout));
121 m_handle->notifiedWake =
false;
127 m_handle->notifiedWake =
true;
128 m_handle->condition.notify_all();
132 virtual void* process(
void) = 0;
133 virtual void finalize(
void) { };
134 bool m_finalizeOnStop;
139 thread_t nativeHandle;
140 volatile bool running;
141 volatile bool stopped;
142 volatile bool notifiedStop;
143 volatile bool notifiedWake;
151 , notifiedStop(
false)
152 , notifiedWake(
false)
159 static void* ThreadHandler(
void* _thread)
161 Thread* thread =
static_cast<Thread*
>(_thread);
166 bool finalize = thread->m_finalizeOnStop;
167 thread->m_handle->mutex.lock();
168 thread->m_handle->running =
true;
169 thread->m_handle->stopped =
false;
170 thread->m_handle->condition.notify_all();
171 thread->m_handle->mutex.unlock();
172 ret = thread->process();
173 thread->m_handle->mutex.lock();
174 thread->m_handle->running =
false;
175 thread->m_handle->stopped =
true;
176 thread->m_handle->condition.notify_all();
177 thread->m_handle->mutex.unlock();