Add function Thread::startAndLock()
This commit is contained in:
parent
938187b2a6
commit
ceec23dfed
2 changed files with 22 additions and 0 deletions
|
@ -14,12 +14,15 @@
|
|||
#ifdef _WINDOWS
|
||||
#include <windows.h>
|
||||
#endif
|
||||
#include "EventHandler.h"
|
||||
|
||||
using namespace TelldusCore;
|
||||
|
||||
class TelldusCore::ThreadPrivate {
|
||||
public:
|
||||
bool running;
|
||||
EventRef threadStarted;
|
||||
Mutex *mutex;
|
||||
#ifdef _WINDOWS
|
||||
HANDLE thread;
|
||||
DWORD threadId;
|
||||
|
@ -31,6 +34,7 @@ public:
|
|||
Thread::Thread() {
|
||||
d = new ThreadPrivate;
|
||||
d->thread = 0;
|
||||
d->mutex = 0;
|
||||
}
|
||||
|
||||
Thread::~Thread() {
|
||||
|
@ -46,6 +50,15 @@ void Thread::start() {
|
|||
#endif
|
||||
}
|
||||
|
||||
void Thread::startAndLock(Mutex *lock) {
|
||||
EventHandler handler;
|
||||
d->threadStarted = handler.addEvent();
|
||||
d->mutex = lock;
|
||||
this->start();
|
||||
handler.waitForAny();
|
||||
d->threadStarted.reset();
|
||||
}
|
||||
|
||||
bool Thread::wait() {
|
||||
if (!d->thread) {
|
||||
return true;
|
||||
|
@ -64,7 +77,14 @@ bool Thread::wait() {
|
|||
void *Thread::exec( void *ptr ) {
|
||||
Thread *t = reinterpret_cast<Thread *>(ptr);
|
||||
if (t) {
|
||||
if (t->d->threadStarted) {
|
||||
t->d->mutex->lock();
|
||||
t->d->threadStarted->signal();
|
||||
}
|
||||
t->run();
|
||||
if (t->d->mutex) {
|
||||
t->d->mutex->unlock();
|
||||
}
|
||||
t->d->running = false;
|
||||
}
|
||||
#ifdef _WINDOWS
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue