Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool ThreadCondition::timedWait(Mutex& mutex, double secondsToWait)
- {
- if (secondsToWait < 0.0) {
- wait(mutex);
- return true;
- }
- int intervalSeconds = static_cast<int>(secondsToWait);
- int intervalMicroseconds = static_cast<int>((secondsToWait - intervalSeconds) * 1000000.0);
- #if !COMPILER(MSVC)
- // Current time comes in sec/microsec
- timeval currentTime;
- gettimeofday(¤tTime, NULL);
- // Target time comes in sec/nanosec
- timespec targetTime;
- targetTime.tv_sec = currentTime.tv_sec + intervalSeconds;
- targetTime.tv_nsec = (currentTime.tv_usec + intervalMicroseconds) * 1000;
- #else
- // Windows lacks gettimeofday
- double currentTime = getCurrentUTCTimeWithMicroseconds();
- time_t currentTimeSeconds = static_cast<time_t>(currentTime);
- long currentTimeNanoseconds = static_cast<long>((currentTime - currentTimeSeconds) * 1000000000.0);
- targetTime.tv_sec = currentTimeSeconds + intervalSeconds;
- targetTime.tv_nsec = currentTimeNanoseconds + intervalMicroseconds * 1000.0;
- #endif
- if (targetTime.tv_nsec > 1000000000) {
- targetTime.tv_nsec -= 1000000000;
- targetTime.tv_sec++;
- }
- return pthread_cond_timedwait(&m_condition, &mutex.impl(), &targetTime) == 0;
- }
Add Comment
Please, Sign In to add comment