diff --git a/src/lib/utils/thread.cpp b/src/lib/utils/thread.cpp index ee1f0d56..d34d40d6 100755 --- a/src/lib/utils/thread.cpp +++ b/src/lib/utils/thread.cpp @@ -93,11 +93,18 @@ bool WaitThread::join() { return Thread::join(); } -bool WaitThread::Wait(int seconds) { +bool WaitThread::Wait(int seconds, int millis) { pthread_mutex_lock(&m_mutex); struct timespec t; clockGettime(&t); t.tv_sec += seconds; + long newMillis = t.tv_nsec/1000000 + millis; + if (newMillis >= 1000) { + t.tv_sec += newMillis / 1000; + t.tv_nsec = (newMillis%1000) * 1000000; // rounds down to whole millis + } else { + t.tv_nsec += millis * 1000000; + } pthread_cond_timedwait(&m_cond, &m_mutex, &t); pthread_mutex_unlock(&m_mutex); return isRunning(); @@ -120,11 +127,11 @@ bool NotifiableThread::waitNotified(int millis) { if (!m_notified) { struct timespec t; clockGettime(&t); - t.tv_sec += millis / 1000000000; - t.tv_nsec += (millis % 1000000000) * 1000000; - if (t.tv_nsec > 1000000000) { - t.tv_sec++; - t.tv_nsec -= 1000000000; + t.tv_sec += millis / 1000; + t.tv_nsec += (millis % 1000) * 1000000; + if (t.tv_nsec >= 1000000000) { + t.tv_sec += t.tv_nsec / 1000000000; + t.tv_nsec %= 1000000000; } pthread_cond_timedwait(&m_cond, &m_mutex, &t); } diff --git a/src/lib/utils/thread.h b/src/lib/utils/thread.h index 6cd9f2a7..4690929e 100755 --- a/src/lib/utils/thread.h +++ b/src/lib/utils/thread.h @@ -129,9 +129,10 @@ class WaitThread : public Thread { /** * Wait for the specified amount of time. * @param seconds the number of seconds to wait. + * @param millis the optional number of milliseconds to wait. * @return true if this @a WaitThread is still running and not yet stopped. */ - bool Wait(int seconds); + bool Wait(int seconds, int millis = 0); protected: