Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8373987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:49:07+00:00 2026-06-09T14:49:07+00:00

I was reading through my STL implementation (standard-issue g++ 4.6.2 ) and came across

  • 0

I was reading through my STL implementation (standard-issue g++ 4.6.2) and came across this bit of race condition inside of condition_variable:

template<typename _Rep, typename _Period>
cv_status
wait_for(unique_lock<mutex>& __lock,
         const chrono::duration<_Rep, _Period>& __rtime)
{
    return wait_until(__lock, __clock_t::now() + __rtime);
}

Because __clock_t is an std::chrono::system_clock, we are tied to the whims of things like NTP (if the clock is moved back by a day after __clock_t::now() + __rtime, then we’ll wait for a day).

The C++ standard (30.5.1) appears to get it right:

26

Effects: as if

return wait_until(lock, chrono::steady_clock::now() + rel_time);

Boost’s condition_variable implementation has the same problem:

template<typename duration_type>
bool timed_wait(unique_lock<mutex>& m,duration_type const& wait_duration)
{
    return timed_wait(m,get_system_time()+wait_duration);
}

In fact, the underlying pthreads implementation seems to be the problem:

int pthread_cond_timedwait(pthread_cond_t *restrict cond,
   pthread_mutex_t *restrict mutex,
   const struct timespec *restrict abstime);

because abstime is specified as “system time,” not a monotonic clock.

So my question is: How would one implement something like std::condition_variable::wait_for correctly? Is there an existing implementation that gets this right? Or am I missing something?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:49:09+00:00Added an answer on June 9, 2026 at 2:49 pm

    The trick is to use a pthread_condattr_setclock to tell the pthread_condattr_t to use CLOCK_MONOTONIC. The C code for doing this is pretty simple:

    #include <time.h>
    #include <pthread.h>
    
    #include <errno.h>
    #include <stdio.h>
    
    int main()
    {
        // Set the clock to be CLOCK_MONOTONIC
        pthread_condattr_t attr;
        pthread_condattr_init(&attr);
        if (int err = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC))
        {
            printf("Error setting clock: %d\n", err);
        }
    
        // Now we can initialize the pthreads objects with that condattr
        pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
        pthread_cond_t  cond;
        pthread_cond_init(&cond, &attr);
    
        // when getting the time, we must poll from CLOCK_MONOTONIC
        struct timespec timeout;
        struct timespec now;
        clock_gettime(CLOCK_MONOTONIC, &now);
        timeout.tv_sec = now.tv_sec + 5;
        timeout.tv_nsec = now.tv_nsec;
    
        // business as usual...
        pthread_mutex_lock(&mutex);
        int rc = pthread_cond_timedwait(&cond, &mutex, &timeout);
        if (rc == ETIMEDOUT)
            printf("Success!\n");
        else
            printf("Got return that wasn't timeout: %d\n", rc);
        pthread_mutex_unlock(&mutex);
    
        return 0;
    }
    

    I’m going to leave this open for a while because somebody might have an easier answer. The thing I’m not happy about here is that it means a wait_until is rather difficult to implement with a real-time clock (my best solution to that is to convert the provided Clock in the time_point into the steady_clock‘s time and go from there…it is still subject to time-change race conditions, but if you’re specifying a timeout in real time, you’re already making a terrible mistake).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was reading through K&R and i came across this example about uncertainty in
Reading through this , I came to the bit on default values for function
Whilst reading through the book The well grounded Rubyist, I came across some strange
Reading through wikipedia I came across the concept of Sprints in Agile development. From
I was doing some reading through the sun java tutorials, and I came across
Reading through some code, I came across the use of !0 and !1 .
Whilst reading through K&R, I came across the integer to string function. I gave
Reading through The Apache Modules Book , I come across this claim in part
Reading through this excellent article about safe construction techniques by Brain Goetz, I got
I'm currently reading through this jquery masking plugin to try and understand how it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.