How do you add a timed delay to a C++ program?

后端 未结 14 2102
情话喂你
情话喂你 2020-11-28 02:11

I am trying to add a timed delay in a C++ program, and was wondering if anyone has any suggestions on what I can try or information I can look at?

I wish I had more

相关标签:
14条回答
  • 2020-11-28 02:19

    Syntax:

    void sleep(unsigned seconds);

    sleep() suspends execution for an interval (seconds). With a call to sleep, the current program is suspended from execution for the number of seconds specified by the argument seconds. The interval is accurate only to the nearest hundredth of a second or to the accuracy of the operating system clock, whichever is less accurate.

    0 讨论(0)
  • 2020-11-28 02:20

    to delay output in cpp for fixed time, you can use the Sleep() function by including windows.h header file syntax for Sleep() function is Sleep(time_in_ms) as

    cout<<"Apple\n";
    Sleep(3000);
    cout<<"Mango";
    

    OUTPUT. above code will print Apple and wait for 3 seconds before printing Mango.

    0 讨论(0)
  • 2020-11-28 02:21
    #include <unistd.h>
    usleep(3000000);
    

    This will also sleep for three seconds. You can refine the numbers a little more though.

    0 讨论(0)
  • 2020-11-28 02:22

    Many others have provided good info for sleeping. I agree with Wedge that a sleep seldom the most appropriate solution.

    If you are sleeping as you wait for something, then you are better off actually waiting for that thing/event. Look at Condition Variables for this.

    I don't know what OS you are trying to do this on, but for threading and synchronisation you could look to the Boost Threading libraries (Boost Condition Varriable).

    Moving now to the other extreme if you are trying to wait for exceptionally short periods then there are a couple of hack style options. If you are working on some sort of embedded platform where a 'sleep' is not implemented then you can try a simple loop (for/while etc) with an empty body (be careful the compiler does not optimise it away). Of course the wait time is dependant on the specific hardware in this case. For really short 'waits' you can try an assembly "nop". I highly doubt these are what you are after but without knowing why you need to wait it's hard to be more specific.

    0 讨论(0)
  • 2020-11-28 02:23

    I found that "_sleep(milliseconds);" (without the quotes) works well for Win32 if you include the chrono library

    E.g:

    #include <chrono>
    
    using namespace std;
    
    main
    {
        cout << "text" << endl;
        _sleep(10000); // pauses for 10 seconds
    }
    

    Make sure you include the underscore before sleep.

    0 讨论(0)
  • 2020-11-28 02:27

    An updated answer for C++11:

    Use the sleep_for and sleep_until functions:

    #include <chrono>
    #include <thread>
    
    int main() {
        using namespace std::this_thread; // sleep_for, sleep_until
        using namespace std::chrono; // nanoseconds, system_clock, seconds
    
        sleep_for(nanoseconds(10));
        sleep_until(system_clock::now() + seconds(1));
    }
    

    With these functions there's no longer a need to continually add new functions for better resolution: sleep, usleep, nanosleep, etc. sleep_for and sleep_until are template functions that can accept values of any resolution via chrono types; hours, seconds, femtoseconds, etc.

    In C++14 you can further simplify the code with the literal suffixes for nanoseconds and seconds:

    #include <chrono>
    #include <thread>
    
    int main() {
        using namespace std::this_thread;     // sleep_for, sleep_until
        using namespace std::chrono_literals; // ns, us, ms, s, h, etc.
        using std::chrono::system_clock;
    
        sleep_for(10ns);
        sleep_until(system_clock::now() + 1s);
    }
    

    Note that the actual duration of a sleep depends on the implementation: You can ask to sleep for 10 nanoseconds, but an implementation might end up sleeping for a millisecond instead, if that's the shortest it can do.

    0 讨论(0)
提交回复
热议问题