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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:36:07+00:00 2026-06-01T13:36:07+00:00

I am attempting to transition an existing program to use the new time facilities

  • 0

I am attempting to transition an existing program to use the new time facilities in C++11 from (homegrown) existing time classes. For real-time processing it is clear how to map the C++11 functionality into the homegrown time classes. It is less clear how the C++11 chrono time facilities can be used to support a non-real time mode (e.g., a “run as fast as you can batch mode”, a “run at quarter speed demonstration mode”, etc.) which the homegrown classes support. Is this accomplished via defining special clocks that are mapping the wall time to the “playback” speed properly? Any help appreciated and an example would be fantastic.

For example, the code I will transitioning has constructs such as

MessageQueue::poll( Seconds( 1 ) );

or

sleep( Minutes( 2 ) );

where the Seconds or Minutes object is aware of the speed at which the program is being run at to avoid having to use a multiplier or conversion function all of over the place like

MessageQueue::poll( PlaybackSpeed * Seconds( 1 ) );

or

MessageQueue::poll( PlaybackSpeed( Seconds( 1 ) ) );

What I was hoping was possible was to obtain the same sort of behavior with std::chrono::duration and std::chrono::time_point by providing a custom clock.

  • 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-01T13:36:08+00:00Added an answer on June 1, 2026 at 1:36 pm

    Whether or not making your own clock will be sufficient depends on how you use the time durations you create. For example if you wanted to run at half speed but somewhere called:

    std::this_thread::sleep_for(std::chrono::minutes(2));
    

    The duration would not be adjusted. Instead you’d need to use sleep_until and provide a time point that uses your ‘slow’ clock. But making a clock that runs slow is pretty easy:

    template<typename Clock,int slowness>
    struct slow_clock {
        using rep = typename Clock::rep;
        using period = typename Clock::period;
        using duration = typename Clock::duration;
        using time_point = std::chrono::time_point<slow_clock>;
    
        constexpr static bool is_steady = Clock::is_steady;
    
        static time_point now() {
            return time_point(start_time.time_since_epoch() + ((Clock::now() - start_time)/slowness));
        }
    
        static const typename Clock::time_point start_time;
    };
    
    template<typename Clock,int slowness>
    const typename Clock::time_point
    slow_clock<Clock,slowness>::start_time = Clock::now();
    

    The time_points returned from now() will appear to advance at a slower rate relative to the clock you give it. For example here’s a program so you can watch nanoseconds slowly tick by:

    int main() {
        using Clock = slow_clock<std::chrono::high_resolution_clock,500000000>;
        for(int i=0;i<10;++i) {
            std::this_thread::sleep_until(Clock::now()
                                          + std::chrono::nanoseconds(1));
            std::cout << "tick\n";
        }
    }
    

    All of the functions you implement, like MessageQueue::poll(), will probably need to be implemented in terms of a global clock typedef.

    Of course none of this has anything to do with with how fast the program actually runs, except insofar as you’re slowing down the program based on them. Functions that time out will take longer, sleep_until will take be longer, but operations that don’t wait for some time point in the future will simply appear to be faster.

    // appears to run a million times faster than normal according to (finish-start)
    auto start = slow_clock<steady_clock,1000000>::now();
    do_slow_operation();
    auto finish = slow_clock<steady_clock,1000000>::now();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Attempting to use the data series from this example no longer passes the JSONLint
I am attemting to transition from one UIView to another, when the user rotates
Attempting to print out a list of values from 2 different variables that are
Attempting to deploy a MOSS solution to a UAT server from dev server for
Attempting to use XStream's JavaBeanConverter and running into an issue. Most likely I'm missng
I'm creating a Java applet from a large scale pre-existing project (Vizster). I am
In my code I'm attempting to use a transaction using TransactionScope with Entity Framework.
I am attempting to insert data into a local SQLITE database file from a
I'm attempting to upload a file into a jsp and then use the file
I am attempting to write the results from an SQL query into text files.

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.