Browse Source

Use a signal to wait for thread start

pull/6/head
falkTX 9 years ago
parent
commit
94f2f5087a
1 changed files with 9 additions and 7 deletions
  1. +9
    -7
      distrho/extra/Thread.hpp

+ 9
- 7
distrho/extra/Thread.hpp View File

@@ -37,7 +37,8 @@ protected:
* Constructor. * Constructor.
*/ */
Thread(const char* const threadName = nullptr) noexcept Thread(const char* const threadName = nullptr) noexcept
: fLock(false),
: fLock(),
fSignal(fLock),
fName(threadName), fName(threadName),
#ifdef PTW32_DLLPORT #ifdef PTW32_DLLPORT
fHandle({nullptr, 0}), fHandle({nullptr, 0}),
@@ -92,7 +93,7 @@ public:
// check if already running // check if already running
DISTRHO_SAFE_ASSERT_RETURN(! isThreadRunning(), true); DISTRHO_SAFE_ASSERT_RETURN(! isThreadRunning(), true);


const MutexLocker cml(fLock);
fLock.lock();


fShouldExit = false; fShouldExit = false;


@@ -109,11 +110,11 @@ public:
_copyFrom(handle); _copyFrom(handle);


// wait for thread to start // wait for thread to start
fLock.lock();

fSignal.wait();
return true; return true;
} }


fLock.unlock();
return false; return false;
} }


@@ -210,6 +211,7 @@ public:


private: private:
Mutex fLock; // Thread lock Mutex fLock; // Thread lock
Signal fSignal; // Thread start wait signal
const String fName; // Thread name const String fName; // Thread name
volatile pthread_t fHandle; // Handle for this thread volatile pthread_t fHandle; // Handle for this thread
volatile bool fShouldExit; // true if thread should exit volatile bool fShouldExit; // true if thread should exit
@@ -258,11 +260,11 @@ private:
*/ */
void _runEntryPoint() noexcept void _runEntryPoint() noexcept
{ {
// report ready
fLock.unlock();

setCurrentThreadName(fName); setCurrentThreadName(fName);


// report ready
fSignal.broadcast();

try { try {
run(); run();
} catch(...) {} } catch(...) {}


Loading…
Cancel
Save