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 8133301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:33:44+00:00 2026-06-06T09:33:44+00:00

In C++11, the *_until timeout functions behave as expected only if a steady clock

  • 0

In C++11, the *_until timeout functions behave “as expected” only if a steady clock (i.e., one that moves only forward at an unchanging rate) is used. Because system_clock is not a steady clock, that means that code like this can behave quite surprisingly:

using namespace std::chrono;
std::this_thread::sleep_until(system_clock::now() + seconds(10));

This will cause the current thread to sleep for 10 seconds unless the system clock is adjusted during the sleep period, e.g., for daylight savings time. If the clock is set back an hour during the sleep, the current thread will sleep for an hour and 10 seconds.

From what I can tell, every *_until timeout function in C++11 has a corresponding *_for function that takes a duration instead of a timepoint. For example, the above code can be rewritten as follows:

using namespace std::chrono;
std::this_thread::sleep_for(seconds(10));

The *_for functions shouldn’t have to worry about clocks that get adjusted while the function is executing, because they just say how long to wait, not what time it’s supposed to be when the wait is over.

This issue affects more than sleep functions, as the same is true for timeout-based waits on futures and try_lock functions.

The only situation in which I can envision it making sense to use an *_until function with an unsteady clock would be when you want to take clock adjustments into account, e.g., you want to sleep until next Wednesday at 3:30AM, even if there’s a change to or from daylight savings time between now and then. Are there other situations where *_until functions make more sense than *_for functions? If not, is it safe to say that, in general, the *_for timeout functions should be preferred over the *_until functions?

  • 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-06T09:33:45+00:00Added an answer on June 6, 2026 at 9:33 am

    The xxx_until calls are for when you have deadline. The typical use case is where you have a hard time limit for a section of code which either contains multiple waits, or where the time consumed by each step prior to the wait is unpredictable.

    e.g.

    void foo() {
      std::chrono::steady_clock::time_point const timeout=
        std::chrono::steady_clock::now()+std::chrono::milliseconds(30);
    
      do_something_which_takes_some_time();
    
      if(some_future.wait_until(timeout)==std::future_status::ready)
        do_something_with(some_future.get());
    }
    

    This will only process the value from some_future if it is ready within 30ms from the start, including the time taken for do_something_which_takes_some_time().

    As in this example, most use cases of the xxx_until functions will use a steady clock in order to have a predictable timeout.

    The only case where I can imagine using the xxx_until functions with a non-steady clock (such as std::chrono::system_clock) is where the timeout is user-visible, and depends on the value of the chosen clock. An alarm clock or reminder program is one example, a backup program that runs “at midnight” is another.

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

Sidebar

Related Questions

Until recently, I believed that the .NET runtime only increases the reference count of
i have a jquery function that when clicked produces a set timeout on making
I have some functions in my Javascript code that will perform long operations such
Until now I've always used the ASP.NET MVC framework source for debugging ASP.NET MVC.
Until now I've been only writing console applications but I need to write a
Until now I've been used to using DAOs to retrieve information from databases. Other
I have a web-app that has a master mysql db and four slave dbs.
In my application I use a function to show GtkInfoBars with a timeout (as
We are creating a gadget for the opensocial API 0.7. In some functions we
Possible Duplicate: setTimeout and “this” in JavaScript I am trying to put a timeout

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.